anthropic

    Thin Anthropic Messages API client for MoonBit built on gaato/mttp_core.

    anthropic
    llm
    sdk
    messages
    Download zip
    Author
    Version
    0.18.0
    License
    Apache-2.0
    Last updated
    13 days ago
    Downloads
    318

    #gaato/anthropic

    Call Anthropic's Messages API from MoonBit: send a message, stream text as it arrives, and let the model call your own functions with the results carried into the next turn. The client also counts input tokens and lists or retrieves models. base_url selects a server that implements the same API.

    The client does no I/O of its own. It sends through an HTTP Backend and reads time from a Clock that you pass in, so it builds for every MoonBit target and a test can hand it a fake backend. gaato/mttp supplies the real ones.

    Unofficial, and not affiliated with Anthropic. A minor release can change the API; the changelog lists each change.

    What it does not cover: the beta APIs, message batches and the Files API.

    #Install

    moon add gaato/anthropic moon add gaato/mttp

    An application imports gaato/anthropic and gaato/mttp. gaato/mttp supplies the network, the clock and the timer the client needs. A library that wraps this SDK adds gaato/mttp_core instead, and takes those three from its caller, so its tests can use a fake backend.

    #A complete program

    answer sends one question and returns the reply's text:

    ///|
    pub async fn answer(
    client : @anthropic.Anthropic,
    model : String,
    question : String,
    ) -> Result[String, @anthropic.AnthropicFailure] {
    let request = @anthropic.MessageRequest::new(model~, max_tokens=1024, messages=[
    @anthropic.Message::user(question),
    ])
    match client.create_message(request) {
    Ok(message) => Ok(message.text())
    Err(failure) => Err(failure)
    }
    }

    A command-line program builds the client once, from mttp's backend, clock and timer, and closes the backend when it is done. Import gaato/mttp, moonbitlang/core/env and moonbitlang/core/debug in its moon.pkg:

    ///|
    fn main {
    @mttp.run(async fn() {
    guard @env.get_env_var("ANTHROPIC_API_KEY") is Some(api_key) else {
    println("Set ANTHROPIC_API_KEY")
    return
    }
    guard @env.get_env_var("ANTHROPIC_MODEL") is Some(model) else {
    println("Set ANTHROPIC_MODEL to a model your account can use")
    return
    }
    let backend = @mttp.backend()
    defer backend.close()
    let client = @anthropic.Anthropic::new(
    api_key~,
    backend,
    @mttp.clock(),
    timer=@mttp.timer(),
    )
    match answer(client, model, "Write a haiku about rivers.") {
    Ok(text) => println(text)
    Err(failure) => println(@debug.to_string(failure))
    }
    })
    }

    Run it with moon run . --target native. Build one Anthropic value and reuse it, so calls share connections. A system prompt goes in MessageRequest::new(system=...). To keep a conversation, pass the earlier Message::user and Message::assistant turns in messages with each request.

    Message::user_blocks takes content blocks from gaato/anthropic/gen instead of a string, so a user turn can carry images (base64, URL or file ID) and PDF or plain-text documents beside its text.

    #Stream text as it arrives

    ///|
    /// Streams a message, passing each piece of text to `on_text` as it arrives.
    pub async fn stream_text(
    client : @anthropic.Anthropic,
    model~ : String,
    on_text : (String) -> Unit,
    ) -> Result[Unit, @anthropic.AnthropicFailure] {
    client.stream_message(
    @anthropic.MessageRequest::new(model~, max_tokens=1024, messages=[
    @anthropic.Message::user("Write a haiku about rivers."),
    ]),
    event => if event is TextDelta(text~, ..) { on_text(text) },
    )
    }

    The callback receives every MessageEvent. For a streamed tool call, ToolUseAccumulator joins the partial JSON arguments. It grows with what you feed it, so bound the stream as well as max_tokens.

    #Let the model call your function

    Give the model a tool definition, and supply the function in your application. This example allows one stock lookup, then asks for a final reply. If the model answers immediately, or asks the user for a SKU, the first message goes straight back to the caller.

    ///|
    pub async fn answer_with_stock(
    client : @anthropic.Anthropic,
    model : String,
    question : String,
    stock_for : (String) -> String,
    ) -> Result[@anthropic.MessageResponse, @anthropic.AnthropicFailure] {
    let tool = @anthropic.ToolDef::new(
    name="lookup_stock",
    description="Look up availability for a product SKU",
    input_schema={
    "type": "object",
    "properties": { "sku": { "type": "string" } },
    "required": ["sku"],
    },
    )
    let user = @anthropic.Message::user(question)
    let request = @anthropic.MessageRequest::new(
    model~,
    max_tokens=1024,
    messages=[user],
    tools=[tool],
    tool_choice=@anthropic.tool_choice_auto(disable_parallel_tool_use=true),
    )
    let first = match client.create_message(request) {
    Ok(reply) => reply
    Err(failure) => return Err(failure)
    }
    guard first.tool_uses() is [(id, name, input)] else { return Ok(first) }
    let result = match (name, input) {
    ("lookup_stock", { "sku": String(sku), .. }) =>
    @anthropic.Message::tool_result(tool_use_id=id, content=stock_for(sku))
    _ =>
    @anthropic.Message::tool_result(
    tool_use_id=id,
    content="Expected lookup_stock with a string SKU.",
    is_error=true,
    )
    }
    client.create_message(
    @anthropic.MessageRequest::new(
    model~,
    max_tokens=1024,
    tools=[tool],
    tool_choice=@anthropic.tool_choice_none(),
    messages=[user, @anthropic.Message::from_response(first), result],
    ),
    )
    }

    Read text() from the returned message. The second request carries the original user message, the assistant's whole tool-use turn (Message::from_response) and a result with the matching tool_use_id. Keeping only the assistant's text would lose the tool-use block, and the API would refuse the result.

    #Other operations

    The client also has count_tokens, list_models, retrieve_model and paginate_models. MessageResponse.raw keeps the full JSON for fields the typed view leaves out. MessageRequest takes thinking, output configuration, metadata and stop sequences, and its extra map adds fields it does not type. create_message_params and stream_message_params take the generated CreateMessageParams from gaato/anthropic/gen for the whole request vocabulary. auth_header=Bearer sends the key as authorization: Bearer for gateways that expect it, with base_url set to the gateway's root.

    #Failures, retries and deadlines

    A call returns Err for a failure the server reported, and raises for a failure to reach it:

    • Err(Validation(...)): invalid input, caught before sending.
    • Err(Response(...)): an HTTP or decoding failure. api_error(failure) extracts Anthropic's error envelope from it.
    • Raised TransportError: the network failed, a deadline expired, or a body passed its limit.

    SettingDefaultNotes
    Retries2, on 408, 409, 429, 5xx (529 overloaded included), timeouts and network failures, POST included; x-should-retry is obeyedAs in Anthropic's official Python SDK. A retried message can be billed twice. retry=@mttp.RetryPolicy::conservative() retries a POST only when nothing was sent.
    Retry-AfterWaited out, however longBound the call with total_timeout_ms: a wait that would outlast it is not begun.
    timeout_ms600 s per attemptNeeds timer=.
    total_timeout_msnoneNeeds timer=. Covers retries, waits and stream callbacks.
    Buffered body64 MiBmax_body_bytes=
    Stream event16 MiB each

    Once a stream has started delivering events, it is never replayed.

    The SDK marks x-api-key and authorization as credentials on each request, including with from_client. A redirect strips them when the host, port or scheme changes, except for a same-host HTTP port 80 to HTTPS port 443 upgrade. HTTPS to HTTP is refused by default.

    #Packages

    PackageWhat it is
    gaato/anthropicThe Anthropic client, MessageRequest, MessageResponse, MessageEvent and tool helpers
    gaato/anthropic/genTypes and operations generated from the official Python SDK's OpenAPI document

    #License

    Apache-2.0

    CreateMessageParams

    The full Messages request, for create_message_params and stream_message_params.

    InputContentBlock

    A content block of an input message: text, image, document, tool use or result, and others.

    ListResponseModelInfo

    One page of list_models.

    MessageDelta

    The top-level changes in a message_delta stream event, such as the stop reason.

    MessageDeltaUsage

    The cumulative token counts in a message_delta stream event.

    MessageStreamEvent

    A raw Messages stream event as the server sends it.

    Metadata

    Request metadata: an opaque id of the end user.

    ModelInfo

    A model as list_models and retrieve_model return it.

    ModelsListLifecycleItem

    A model lifecycle stage that list_models can filter by.

    OutputConfig

    Output settings of a request: the effort level and a structured output format.

    StopReason

    Why the model stopped generating, as in the stop_reason of a MessageResponse.

    ThinkingConfigParam

    Whether and how the model thinks before answering.

    ToolChoice

    How the model may use the request's tools; the tool_choice_* helpers build the common ones.

    Usage

    Token counts billed for a message.

    Anthropic

    pub struct Anthropic {
    // private fields
    }

    An Anthropic API client backed by the transport-independent SDK runtime.

    Request and response shapes come from the generated gen package, which is produced from the vendored first-party OpenAPI document; this facade adds a small stable vocabulary on top of it.
    impl Debug for Anthropic

    Anthropic::count_tokens

    async fn Anthropic::count_tokens(self : Anthropic, request : MessageRequest) -> Result[Int, AnthropicFailure]

    Counts input tokens without creating a message.

    Anthropic::create_message

    async fn Anthropic::create_message(self : Anthropic, request : MessageRequest) -> Result[MessageResponse, AnthropicFailure]

    Creates and buffers one message.

    Anthropic::create_message_params

    Creates a buffered message from generated parameters. stream is ignored.

    Anthropic::from_client

    fn Anthropic::from_client(client :
    Client
    , version? : String) -> Anthropic

    Wraps a preconfigured runtime client for compatible APIs and tests.

    From mttp_core 0.3.0, each request marks x-api-key and authorization. They are stripped when the host, port or scheme changes, except on a same-host HTTP port 80 to HTTPS port 443 upgrade. HTTPS to HTTP is refused by default.

    Anthropic::list_models

    Lists a page of models. Follow has_more with after_id=last_id.

    Anthropic::new

    fn Anthropic::new(api_key~ : String, backend : &
    Backend
    , clock : &
    Clock
    , base_url? : String, version? : String, auth_header? : AuthHeader, retry? :
    RetryPolicy
    , timer? : &
    Timer
    , timeout_ms? : Int, max_body_bytes? : Int, total_timeout_ms? : Int) -> Anthropic

    Creates an Anthropic client with the selected authentication header.

    base_url is the server root: generated paths already start with /v1, so an Anthropic-compatible gateway is addressed by its root as well (for example https://openrouter.ai/api).

    retry defaults to the official SDK's behaviour (anthropic-sdk-python, _base_client.py): an SDK-local policy with two retries of 408, 409, 429 and every 5xx (529 overloaded included), and of a timeout or network failure, for every method including POST, so a message can be generated twice; x-should-retry is obeyed, and any Retry-After is waited out however long, so set total_timeout_ms to bound a call. Pass retry=@client.RetryPolicy::conservative() to retry a POST only on failures that prove nothing was sent, unless explicitly declared replay-safe.

    With a timer (such as @mttp.timer()), each attempt is limited to timeout_ms (600 s, the official default) and, when total_timeout_ms is given, the whole call, retries and their waits included, to that. A total deadline requires Timer and also covers admission and stream callbacks. Backend phase deadlines can finish attempts earlier.

    Redirects follow the runtime's default policies; see from_client for how the SDK marks its credentials.

    Anthropic::paginate_models

    Iterates model pages following has_more and last_id.

    Anthropic::retrieve_model

    async fn Anthropic::retrieve_model(self : Anthropic, model_id : String) -> Result[
    ModelInfo
    , AnthropicFailure]

    Retrieves one model by identifier or alias.

    Anthropic::stream_message

    async fn Anthropic::stream_message(self : Anthropic, request : MessageRequest, f : async (MessageEvent) -> Unit) -> Result[Unit, AnthropicFailure]

    Dispatches message events in order, reading through EOF and closing on failure.

    Anthropic::stream_message_params

    Streams generated events. stream is forced on; future event kinds remain Unknown.

    Anthropic::to_repr

    Redacted debug representation of an Anthropic client.

    AnthropicFailure

    pub(all) enum AnthropicFailure {
    Response(
    ResponseFailure
    )
    Validation(String)
    } derive(
    Debug
    )

    An expected SDK failure. TransportError and unknown user errors remain raised.

    AnthropicFailure::to_repr

    Formats an expected failure with its original response metadata.

    ApiErrorBody

    pub(all) struct ApiErrorBody {
    type_ : String
    message : String
    } derive(Eq,
    Debug
    )

    The error body returned by the Anthropic API.

    test {
    let error : @anthropic.ApiErrorBody = {
    type_: "invalid_request_error",
    message: "invalid request",
    }
    assert_eq(error.message, "invalid request")
    }

    ApiErrorBody::equal

    Compares API error bodies field by field.

    ApiErrorBody::not_equal

    fn ApiErrorBody::not_equal(x : ApiErrorBody, y : ApiErrorBody) -> Bool

    Compares API error bodies field by field.

    ApiErrorBody::to_repr

    Debug representation of an API error body.

    AuthHeader

    pub(all) enum AuthHeader {
    XApiKey
    Bearer
    } derive(Eq,
    Debug
    )

    Selects the credential header used by the API.

    AuthHeader::equal

    fn AuthHeader::equal(AuthHeader, AuthHeader) -> Bool

    Compares authentication-header choices.

    AuthHeader::not_equal

    fn AuthHeader::not_equal(x : AuthHeader, y : AuthHeader) -> Bool

    Compares authentication-header choices.

    AuthHeader::to_repr

    Debug representation of an authentication-header choice.

    ContentBlock

    pub(all) enum ContentBlock {
    Text(String)
    Thinking(thinking~ : String, signature~ : String)
    RedactedThinking(String)
    ToolUse(id~ : String, name~ : String, input~ :
    Json
    )
    Other(String,
    Json
    )
    } derive(Eq,
    Debug
    )

    One output content block. Blocks beyond text, thinking and tool use keep their JSON; the typed form is on MessageResponse::message.

    ContentBlock::equal

    Compares output content blocks.

    ContentBlock::not_equal

    fn ContentBlock::not_equal(x : ContentBlock, y : ContentBlock) -> Bool

    Compares output content blocks.

    ContentBlock::to_repr

    Debug representation of an output content block.

    InputContent

    pub(all) enum InputContent {
    Text(String)
    Blocks(Array[
    InputContentBlock
    ])
    } derive(Eq,
    Debug
    )

    Input content as plain text or typed content blocks. A block the generated vocabulary does not know is carried losslessly as @gen.InputContentBlock::Unknown.

    InputContent::equal

    Compares input content, including typed blocks.

    InputContent::not_equal

    fn InputContent::not_equal(x : InputContent, y : InputContent) -> Bool

    Compares input content, including typed blocks.

    InputContent::to_repr

    Debug representation of input content.

    Message

    pub(all) struct Message {
    role : Role
    content : InputContent
    } derive(Eq,
    Debug
    )

    One user, assistant or system turn sent to the Messages API.

    Message::assistant

    fn Message::assistant(text : String) -> Message

    Creates an assistant message containing plain text.

    test {
    assert_eq(@anthropic.Message::assistant("hello"), {
    role: @anthropic.Assistant,
    content: @anthropic.Text("hello"),
    })
    }

    Message::assistant_blocks

    Creates an assistant message from typed content blocks.

    Message::equal

    fn Message::equal(Message, Message) -> Bool

    Compares messages field by field.

    Message::from_response

    fn Message::from_response(response : MessageResponse) -> Message

    Turns a response into the assistant turn to send back, block for block: text, thinking (with its signature), tool use and every other block the API returned are replayed as input blocks. A block the request vocabulary cannot represent is passed through as raw JSON.

    Message::not_equal

    fn Message::not_equal(x : Message, y : Message) -> Bool

    Compares messages field by field.

    Message::system

    fn Message::system(text : String) -> Message

    Creates a mid-conversation system message containing plain text. Place it right after a user turn; see Role.

    test {
    assert_eq(@anthropic.Message::system("answer in French"), {
    role: @anthropic.System,
    content: @anthropic.Text("answer in French"),
    })
    }

    Message::to_repr

    Debug representation of a message.

    Message::tool_result

    fn Message::tool_result(tool_use_id~ : String, content~ : String, is_error? : Bool) -> Message

    Creates a user message containing one tool result block.

    test {
    let message = @anthropic.Message::tool_result(
    tool_use_id="toolu_1",
    content="done",
    )
    assert_eq(message.role, @anthropic.User)
    }

    Message::user

    fn Message::user(text : String) -> Message

    Creates a user message containing plain text.

    test {
    assert_eq(@anthropic.Message::user("hello"), {
    role: @anthropic.User,
    content: @anthropic.Text("hello"),
    })
    }

    Message::user_blocks

    Creates a user message from typed content blocks (images, documents, tool results, ...).

    MessageEvent

    pub(all) enum MessageEvent {
    MessageStart(MessageResponse)
    ContentBlockStart(index~ : Int, block~ : ContentBlock)
    TextDelta(index~ : Int, text~ : String)
    ThinkingDelta(index~ : Int, thinking~ : String)
    SignatureDelta(index~ : Int, signature~ : String)
    InputJsonDelta(index~ : Int, partial_json~ : String)
    ContentBlockStop(index~ : Int)
    MessageDelta(delta~ :
    MessageDelta
    , usage~ :
    MessageDeltaUsage
    )
    MessageStop
    Ping
    Error(ApiErrorBody)
    Other(String,
    Json
    )
    } derive(Eq,
    Debug
    )

    One event from a streaming message. Event and delta kinds outside the generated vocabulary retain their raw JSON.

    MessageEvent::equal

    Compares message events, including retained unknown JSON.

    MessageEvent::not_equal

    fn MessageEvent::not_equal(x : MessageEvent, y : MessageEvent) -> Bool

    Compares message events, including retained unknown JSON.

    MessageEvent::to_repr

    Debug representation of a message event.

    MessageRequest

    pub(all) struct MessageRequest {
    model : String
    max_tokens : Int
    messages : Array[Message]
    system : String?
    stop_sequences : Array[String]?
    tools : Array[ToolDef]?
    tool_choice :
    ToolChoice
    ?
    thinking :
    ThinkingConfigParam
    ?
    output_config :
    OutputConfig
    ?
    metadata :
    Metadata
    ?
    extra : Map[String,
    Json
    ]
    } derive(Eq,
    Debug
    )

    Input accepted by buffered and streaming message creation and by token counting.

    The typed fields cover the common surface; extra is merged into the JSON body after the typed fields and validated against the generated schema, so any other documented parameter (service_tier, inference_geo, ...) can be passed without a facade change. For full control build a @gen.CreateMessageParams and use Anthropic::create_message_params.

    MessageRequest::equal

    Compares message requests field by field.

    MessageRequest::new

    fn MessageRequest::new(model~ : String, max_tokens~ : Int, messages~ : Array[Message], system? : String, stop_sequences? : Array[String], tools? : Array[ToolDef], tool_choice? :
    ToolChoice
    , thinking? :
    ThinkingConfigParam
    , output_config? :
    OutputConfig
    , metadata? :
    Metadata
    , extra? : Map[String,
    Json
    ]) -> MessageRequest

    Creates a message request and snapshots its mutable inputs.

    test {
    let request = @anthropic.MessageRequest::new(
    model="claude-opus-5",
    max_tokens=64,
    messages=[@anthropic.Message::user("hello")],
    )
    assert_eq(request.messages.length(), 1)
    }

    MessageRequest::not_equal

    fn MessageRequest::not_equal(x : MessageRequest, y : MessageRequest) -> Bool

    Compares message requests field by field.

    MessageRequest::to_count_tokens_params

    The generated token-counting request for this facade request, without extra.

    MessageRequest::to_params

    The generated request for this facade request, without extra. stream selects server-sent events; the facade sets it for stream_message.

    MessageRequest::to_repr

    Debug representation of a message request.

    MessageResponse

    pub(all) struct MessageResponse {
    id : String
    model : String
    role : Role
    content : Array[ContentBlock]
    stop_reason :
    StopReason
    ?
    usage :
    Usage

    message :
    Message

    raw :
    Json

    } derive(Eq,
    Debug
    )

    A decoded message: the facade view (content), the generated view (message) and the original JSON (raw).

    MessageResponse::equal

    Compares decoded messages field by field.

    MessageResponse::not_equal

    fn MessageResponse::not_equal(x : MessageResponse, y : MessageResponse) -> Bool

    Compares decoded messages field by field.

    MessageResponse::text

    fn MessageResponse::text(self : MessageResponse) -> String

    Concatenates text blocks in response order and ignores other block kinds.

    MessageResponse::to_repr

    Debug representation of a decoded message.

    MessageResponse::tool_uses

    fn MessageResponse::tool_uses(self : MessageResponse) -> Array[(String, String,
    Json
    )]

    The tool uses requested by the response, in content order.

    Role

    pub(all) enum Role {
    User
    Assistant
    System
    } derive(Eq,
    Debug
    )

    A participant in a Messages API conversation. System is a mid-conversation system message, which only some models accept and which must directly follow a user turn; a system prompt for the whole conversation goes in MessageRequest.system. A response is always Assistant.

    Role::equal

    fn Role::equal(Role, Role) -> Bool

    Compares message roles.

    Role::not_equal

    fn Role::not_equal(x : Role, y : Role) -> Bool

    Compares message roles.

    Role::to_repr

    Debug representation of a message role.

    ToolDef

    pub(all) struct ToolDef {
    name : String
    description : String?
    input_schema :
    Json

    strict : Bool?
    } derive(Eq,
    Debug
    )

    A client tool definition for the Messages API.

    test {
    let tool = @anthropic.ToolDef::new(name="lookup", input_schema={
    "type": "object",
    })
    assert_eq(tool.name, "lookup")
    }

    ToolDef::equal

    fn ToolDef::equal(ToolDef, ToolDef) -> Bool

    Compares tool definitions field by field.

    ToolDef::new

    fn ToolDef::new(name~ : String, input_schema~ :
    Json
    , description? : String, strict? : Bool) -> ToolDef

    Creates a client tool definition with a JSON Schema input object.

    ToolDef::not_equal

    fn ToolDef::not_equal(x : ToolDef, y : ToolDef) -> Bool

    Compares tool definitions field by field.

    ToolDef::to_repr

    Debug representation of a tool definition.

    ToolUseAccumulator

    pub struct ToolUseAccumulator {
    // private fields
    }

    Reconstructs streamed tool-use inputs by content block index.

    test {
    let tools = @anthropic.ToolUseAccumulator::new()
    tools.feed(
    @anthropic.ContentBlockStart(
    index=0,
    block=@anthropic.ToolUse(id="toolu_1", name="lookup", input={}),
    ),
    )
    tools.feed(@anthropic.InputJsonDelta(index=0, partial_json="{}"))
    tools.feed(@anthropic.ContentBlockStop(index=0))
    assert_eq(tools.finish()[0].2, {})
    }

    ToolUseAccumulator::feed

    fn ToolUseAccumulator::feed(self : ToolUseAccumulator, event : MessageEvent) -> Unit

    Consumes one message event. Invalid partial JSON is retained without raising.

    ToolUseAccumulator::finish

    Returns completed tool uses ordered by block index. Invalid accumulated JSON raises @json.ParseError here rather than from feed.

    ToolUseAccumulator::new

    Creates an empty tool-use accumulator.

    api_error

    fn api_error(error : AnthropicFailure) -> ApiErrorBody?

    Extracts an Anthropic error envelope from an HTTP status failure. Returns None for transport, decode, configuration, and malformed error bodies.

    tool_choice_any

    fn tool_choice_any(disable_parallel_tool_use? : Bool) ->
    ToolChoice

    Requires the model to call some tool.

    tool_choice_auto

    fn tool_choice_auto(disable_parallel_tool_use? : Bool) ->
    ToolChoice

    Lets the model decide whether to call a tool.

    tool_choice_none

    Forbids tool calls.

    tool_choice_tool

    fn tool_choice_tool(name : String, disable_parallel_tool_use? : Bool) ->
    ToolChoice

    Requires the model to call the named tool.