#sdk #anthropic #artificial-intelligence #define

anthropic-ai-sdk

An unofficial Rust SDK for Anthropic's Claude AI API

26 releases

Uses new Rust 2024

new 0.2.0 Mar 27, 2025
0.1.5 Mar 21, 2025
0.0.19 Mar 1, 2025
0.0.17 Feb 2, 2025
0.0.16 Jan 21, 2025

#447 in Machine learning

Download history 59/week @ 2025-01-08 1211/week @ 2025-01-15 44/week @ 2025-01-22 118/week @ 2025-01-29 30/week @ 2025-02-05 18/week @ 2025-02-12 1/week @ 2025-02-19 486/week @ 2025-02-26 489/week @ 2025-03-05 21/week @ 2025-03-12 121/week @ 2025-03-19

1,117 downloads per month
Used in 2 crates

MIT license

61KB
937 lines

Anthropic Rust SDK

Crates.io Documentation License: MIT

An unofficial Rust SDK for the Anthropic API.

Features

  • Robust async/await implementation using Tokio
  • Comprehensive error handling with detailed error types
  • Built-in pagination support for list operations
  • Token counting utilities for accurate message length estimation
  • Type-safe API with full Rust type definitions
  • Easy-to-use builder patterns for request construction

Installation

cargo add anthropic-ai-sdk

Quick Start

use anthropic_ai_sdk::clients::AnthropicClient;
use anthropic_ai_sdk::types::message::{
    CreateMessageParams, Message, MessageClient, MessageError, RequiredMessageParams, Role,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let anthropic_api_key = std::env::var("ANTHROPIC_API_KEY").unwrap();
    let client = AnthropicClient::new::<MessageError>(anthropic_api_key, "2023-06-01").unwrap();

    // stream(false)
    let body = CreateMessageParams::new(RequiredMessageParams {
        model: "claude-3-5-sonnet-20240620".to_string(),
        messages: vec![Message::new_text(Role::User, "Hello, Claude")],
        max_tokens: 1024,
    });

    match client.create_message(Some(&body)).await {
        Ok(message) => {
            println!("Successfully created message: {:?}", message.content);
        }
        Err(e) => {
            println!("Error: {}", e);
        }
    }

    // stream(true)
    let body = CreateMessageParams::new(RequiredMessageParams {
        model: "claude-3-5-sonnet-20240620".to_string(),
        messages: vec![Message::new_text(Role::User, "Hello, Claude")],
        max_tokens: 1024,
    })
    .with_stream(true);

    match client.create_message_streaming(&body).await {
        Ok(mut stream) => {
            while let Some(result) = stream.next().await {
                match result {
                    Ok(event) => info!("Received event: {:?}", event),
                    Err(e) => error!("Stream error: {}", e),
                }
            }
        }
        Err(e) => {
            error!("Error: {}", e);
        }
    }

    Ok(())
}

Examples

Check out the examples directory for more usage examples:

Note: The examples listed above are only a subset. For additional detailed usage examples, please refer to the examples directory.

API Coverage

  • Models
    • List Models
    • Get a Model
  • Messages
    • Messages
    • Count Message Tokens
  • Message Batches
    • Create a Message Batch
    • Retrieve a Message Batch
    • Retrieve Message Batch Results
    • List Message Batches
    • Cancel a Message Batch
    • Delete a Message Batch
  • Admin API
    • Organization Member Management
      • Get User
      • List Users
      • Update User
      • Remove User
    • Organization Invites
      • Get Invite
      • List Invites
      • Create Invite
      • Delete Invite
    • Workspace Management
      • Get Workspace
      • List Workspaces
      • Update Workspace
      • Create Workspace
      • Archive Workspace
    • Workspace Member Management
      • Get Workspace Member
      • List Workspace Member
      • Get Workspace Member
      • Update Workspace Member
      • Delete Workspace Member
    • API Keys
      • Get API Key
      • List API Keys
      • Update API Keys

Development

Prerequisites

  • Rust 1.85.0 or later
  • An Anthropic API key

Running Tests

cargo test

Running Examples

Set your API key

export ANTHROPIC_API_KEY="your-api-key"

Run an example

cd examples/models/list-models
cargo run 

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Security

If you discover a security vulnerability within this package, please send an e-mail to the maintainers. All security vulnerabilities will be promptly addressed.

Support

For support questions, please use the GitHub Issues.

Dependencies

~9–21MB
~280K SLoC