Hanzo

Rust SDK

The official Hanzo Rust SDK with async support and crypto/DID features.

Rust SDK

The hanzo-ai crate provides an async Rust client for the Hanzo LLM Gateway, with additional support for crypto and decentralized identity (DID) features.

Installation

# Cargo.toml
[dependencies]
hanzo-ai = "0.1"
tokio = { version = "1", features = ["full"] }

Quick Start

use hanzo_ai::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new("your-api-key");

    let response = client
        .chat()
        .completions()
        .create("gpt-4o")
        .message("user", "Hello, world!")
        .send()
        .await?;

    println!("{}", response.choices[0].message.content);

    Ok(())
}

Streaming

use futures::StreamExt;
use hanzo_ai::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new("your-api-key");

    let mut stream = client
        .chat()
        .completions()
        .create("claude-sonnet-4-5-20250929")
        .message("user", "Write a haiku about AI")
        .stream()
        .await?;

    while let Some(chunk) = stream.next().await {
        let chunk = chunk?;
        if let Some(content) = &chunk.choices[0].delta.content {
            print!("{content}");
        }
    }

    Ok(())
}

Function Calling

use hanzo_ai::{Client, Tool, FunctionDef};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new("your-api-key");

    let tools = vec![
        Tool::function(FunctionDef {
            name: "get_weather".into(),
            description: Some("Get weather for a location".into()),
            parameters: json!({
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City name"
                    }
                },
                "required": ["location"]
            }),
        }),
    ];

    let response = client
        .chat()
        .completions()
        .create("gpt-4o")
        .message("user", "What's the weather in Tokyo?")
        .tools(tools)
        .send()
        .await?;

    if let Some(tool_calls) = &response.choices[0].message.tool_calls {
        for call in tool_calls {
            println!("Call {}({})", call.function.name, call.function.arguments);
        }
    }

    Ok(())
}

Multiple Providers

// Switch providers by changing the model name
let openai = client.chat().completions().create("gpt-4o")
    .message("user", "Hello!").send().await?;

let anthropic = client.chat().completions().create("claude-sonnet-4-5-20250929")
    .message("user", "Hello!").send().await?;

let google = client.chat().completions().create("gemini-2.0-flash")
    .message("user", "Hello!").send().await?;

Configuration

use hanzo_ai::{Client, Config};

let client = Client::with_config(Config {
    api_key: "your-api-key".into(),
    base_url: "https://llm.hanzo.ai/v1".into(),  // default
    timeout: std::time::Duration::from_secs(60),
    max_retries: 2,
});

Environment Variables

HANZO_API_KEY=your-api-key
HANZO_BASE_URL=https://llm.hanzo.ai/v1  # optional

Error Handling

use hanzo_ai::Error;

match client.chat().completions().create("gpt-4o")
    .message("user", "Hello!")
    .send()
    .await
{
    Ok(response) => println!("{}", response.choices[0].message.content),
    Err(Error::Api { status, message }) => {
        eprintln!("API error {status}: {message}");
    }
    Err(Error::Network(e)) => {
        eprintln!("Network error: {e}");
    }
    Err(e) => eprintln!("Error: {e}"),
}

Crypto & DID Features

The Rust SDK includes additional support for Web3 and decentralized identity:

use hanzo_ai::crypto::{Did, Verifier};

// Create a DID for authenticated API access
let did = Did::from_keypair(&keypair)?;

// Verify DID-authenticated requests
let verifier = Verifier::new(&did);
let is_valid = verifier.verify(&signed_request)?;

Resources

How is this guide?

Last updated on

On this page