Skip to main content

Crate crimson_crab

Crate crimson_crab 

Source
Expand description

§crimson-crab

The production-grade Rust SDK for Anthropic’s Claude API.

This crate mirrors the Claude wire API exactly (see the type modules under types) and is designed to be forward compatible: content blocks, stop reasons, and other type-tagged or string-valued enums all carry a catch-all variant, so values the SDK has never seen deserialize instead of erroring.

crimson-crab is an independent open-source project and is not affiliated with Anthropic.

§Status

The wire types, the HTTP Client, the Messages, Models, and Batches endpoints, and SSE streaming (client.messages().stream(&req)) are all implemented.

§Quickstart

use crimson_crab::model_ids::CLAUDE_OPUS_4_8;
use crimson_crab::prelude::*;

// Reads the API key from the ANTHROPIC_API_KEY environment variable.
let client = Client::from_env()?;

let request = MessagesRequest::builder()
    .model(CLAUDE_OPUS_4_8)
    .max_tokens(1024)
    .messages(vec![MessageParam::user("Hello, Claude!")])
    .build()?;

let message = client.messages().create(&request).await?;
println!("{}", message.text());

§Building request values

use crimson_crab::model_ids::CLAUDE_OPUS_4_8;
use crimson_crab::prelude::*;

// A conversation turn.
let messages = vec![MessageParam::user("What is the weather in Paris?")];

// A custom tool the model may call.
let tool = Tool::new(
    "get_weather",
    "Get the current weather for a location",
    serde_json::json!({
        "type": "object",
        "properties": {"location": {"type": "string"}},
        "required": ["location"]
    }),
);

assert_eq!(CLAUDE_OPUS_4_8, "claude-opus-4-8");
assert_eq!(messages[0].role, Role::User);
assert_eq!(tool.name, "get_weather");

§Parsing a response

use crimson_crab::prelude::*;

let body = serde_json::json!({
    "id": "msg_01ABC",
    "type": "message",
    "role": "assistant",
    "model": "claude-opus-4-8",
    "content": [{"type": "text", "text": "It is sunny."}],
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {"input_tokens": 12, "output_tokens": 4}
});
let msg: Message = serde_json::from_value(body).unwrap();
assert_eq!(msg.text(), "It is sunny.");
assert_eq!(msg.stop_reason, Some(StopReason::EndTurn));

Re-exports§

pub use client::Client;
pub use client::ClientBuilder;
pub use error::ApiError;
pub use error::Error;
pub use error::Result;
pub use streaming::ContentDelta;
pub use streaming::MessageStream;
pub use streaming::StreamEvent;

Modules§

api
High-level endpoint handles and their request/response types.
client
The Client and its ClientBuilder.
error
Errors returned by the SDK.
model_ids
String constants for the current Claude model ids.
prelude
Commonly used types, re-exported for use crimson_crab::prelude::*;.
streaming
Server-sent-events (SSE) streaming for POST /v1/messages with "stream": true.
types
Wire types for the Anthropic Claude API.