claude

Types

Error type for environment variable lookup failures.

pub type EnvError {
  EnvVarNotSet
  EnvVarEmpty
}

Constructors

  • EnvVarNotSet

    The ANTHROPIC_API_KEY environment variable is not set.

  • EnvVarEmpty

    The ANTHROPIC_API_KEY environment variable is set but empty.

Values

pub fn from_env() -> Result(client.Config, EnvError)

Create a client configuration from the ANTHROPIC_API_KEY environment variable.

Returns Error(EnvVarNotSet) if the environment variable is not set, or Error(EnvVarEmpty) if it is set but empty.

Example

let assert Ok(client) = claude.from_env()
pub fn message(
  client: client.Config,
  prompt: String,
) -> Result(message.Message, error.ApiError)

Send a single message to the API without the agent loop.

This is useful for simple one-shot interactions where tool use is not needed.

Example

let client = claude.new("sk-ant-...")
case claude.message(client, "What is 2 + 2?") {
  Ok(msg) -> io.println(claude.text_content(msg))
  Error(_) -> io.println("API error")
}
pub fn new(api_key: String) -> client.Config

Create a new client configuration with the given API key.

Uses sensible defaults:

  • base_url: “https://api.anthropic.com”
  • model: “claude-sonnet-4-5-20250929”
  • max_tokens: 4096

Example

let client = claude.new("sk-ant-...")
pub fn result_text(result: agent.AgentResult) -> String

Extract the text content from an agent result’s final message.

This is a convenience function that applies text_content to the result’s final message.

Example

let assert Ok(result) = claude.run(client, "Hello", tools)
io.println(claude.result_text(result))
pub fn run(
  client: client.Config,
  prompt: String,
  tools: tool.Registry,
) -> Result(agent.AgentResult, agent.AgentError)

Run an agent loop with the given tool registry.

This is the primary entry point for running an agentic workflow. The agent sends the prompt to the API, and if the model responds with tool calls, it executes them using the typed tools in the registry and feeds the results back. This continues until the model stops calling tools or the maximum number of iterations is reached.

Example

let client = claude.new("sk-ant-...")
let tools = tool.registry()
  |> tool.register(weather_tool())

case claude.run(client, "What's the weather in SF?", tools) {
  Ok(result) -> io.println(claude.result_text(result))
  Error(err) -> io.println("Error!")
}
pub fn run_with_config(
  config: config.AgentConfig,
  prompt: String,
) -> Result(agent.AgentResult, agent.AgentError)

Run an agent loop with a fully customized configuration.

Use this when you need to set system prompts, custom models, thinking budgets, or other advanced options.

Example

let tools = tool.registry()
  |> tool.register(weather_tool())

let cfg =
  config.new(client: client, tools: tools)
  |> config.with_system("You are a helpful assistant.")
  |> config.with_model("claude-opus-4-5-20250929")
  |> config.with_max_iterations(5)

case claude.run_with_config(cfg, "Analyze this data") {
  Ok(result) -> io.println(claude.result_text(result))
  Error(_) -> io.println("Error!")
}
pub fn text_content(msg: message.Message) -> String

Extract and concatenate all text content blocks from a message.

Tool use blocks, thinking blocks, and other non-text blocks are ignored. Multiple text blocks are joined with newlines.

Example

let assert Ok(msg) = claude.message(client, "Hello!")
io.println(claude.text_content(msg))
pub fn version() -> String

The current SDK version.

Search Document