|
| 1 | +# Using Codey as a Library |
| 2 | + |
| 3 | +Codey can be used as a library to create AI agents with custom system prompts and tools in your Rust projects. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add codey to your `Cargo.toml`: |
| 8 | + |
| 9 | +```toml |
| 10 | +[dependencies] |
| 11 | +codey = { git = "https://github.com/tcdent/codey" } |
| 12 | +tokio = { version = "1", features = ["full"] } |
| 13 | +serde_json = "1" |
| 14 | +``` |
| 15 | + |
| 16 | +### Patched Dependencies |
| 17 | + |
| 18 | +Codey uses a patched version of the `genai` crate. To use Codey as a library, you'll need to apply the same patch in your project's `Cargo.toml`: |
| 19 | + |
| 20 | +```toml |
| 21 | +[patch.crates-io] |
| 22 | +genai = { git = "https://github.com/tcdent/codey", branch = "main" } |
| 23 | +``` |
| 24 | + |
| 25 | +Alternatively, clone the codey repository and reference the patched genai locally: |
| 26 | + |
| 27 | +```toml |
| 28 | +[patch.crates-io] |
| 29 | +genai = { path = "../codey/lib/genai" } |
| 30 | +``` |
| 31 | + |
| 32 | +Note: Run `make patch` in the codey repository first to download and patch the dependencies. |
| 33 | + |
| 34 | +## Basic Usage (No Tools) |
| 35 | + |
| 36 | +```rust |
| 37 | +use codey::{Agent, AgentRuntimeConfig, AgentStep, RequestMode, ToolRegistry}; |
| 38 | + |
| 39 | +#[tokio::main] |
| 40 | +async fn main() { |
| 41 | + let mut agent = Agent::new( |
| 42 | + AgentRuntimeConfig::default(), |
| 43 | + "You are a helpful assistant.", |
| 44 | + None, // uses ANTHROPIC_API_KEY env var |
| 45 | + ToolRegistry::empty(), |
| 46 | + ); |
| 47 | + |
| 48 | + agent.send_request("What is the capital of France?", RequestMode::Normal); |
| 49 | + |
| 50 | + while let Some(step) = agent.next().await { |
| 51 | + match step { |
| 52 | + AgentStep::TextDelta(text) => print!("{}", text), |
| 53 | + AgentStep::Finished { .. } => break, |
| 54 | + AgentStep::Error(e) => { |
| 55 | + eprintln!("Error: {}", e); |
| 56 | + break; |
| 57 | + } |
| 58 | + _ => {} |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Custom Tools |
| 65 | + |
| 66 | +You can define custom tools using `SimpleTool` and handle their execution yourself. |
| 67 | + |
| 68 | +### Defining Tools |
| 69 | + |
| 70 | +```rust |
| 71 | +use codey::{SimpleTool, ToolRegistry}; |
| 72 | +use serde_json::json; |
| 73 | +use std::sync::Arc; |
| 74 | + |
| 75 | +// Define a tool |
| 76 | +let weather_tool = SimpleTool::new( |
| 77 | + "get_weather", |
| 78 | + "Get the current weather for a location", |
| 79 | + json!({ |
| 80 | + "type": "object", |
| 81 | + "properties": { |
| 82 | + "location": { |
| 83 | + "type": "string", |
| 84 | + "description": "City name, e.g. 'San Francisco'" |
| 85 | + } |
| 86 | + }, |
| 87 | + "required": ["location"] |
| 88 | + }), |
| 89 | +); |
| 90 | + |
| 91 | +// Register tools |
| 92 | +let mut tools = ToolRegistry::empty(); |
| 93 | +tools.register(Arc::new(weather_tool)); |
| 94 | +``` |
| 95 | + |
| 96 | +### Handling Tool Calls |
| 97 | + |
| 98 | +When the LLM wants to use a tool, you'll receive an `AgentStep::ToolRequest`. You must execute the tool and submit the result back to the agent: |
| 99 | + |
| 100 | +```rust |
| 101 | +use codey::{Agent, AgentRuntimeConfig, AgentStep, RequestMode, SimpleTool, ToolCall, ToolRegistry}; |
| 102 | +use serde_json::json; |
| 103 | +use std::sync::Arc; |
| 104 | + |
| 105 | +#[tokio::main] |
| 106 | +async fn main() { |
| 107 | + // Set up tools |
| 108 | + let weather_tool = SimpleTool::new( |
| 109 | + "get_weather", |
| 110 | + "Get the current weather for a location", |
| 111 | + json!({ |
| 112 | + "type": "object", |
| 113 | + "properties": { |
| 114 | + "location": { "type": "string" } |
| 115 | + }, |
| 116 | + "required": ["location"] |
| 117 | + }), |
| 118 | + ); |
| 119 | + |
| 120 | + let mut tools = ToolRegistry::empty(); |
| 121 | + tools.register(Arc::new(weather_tool)); |
| 122 | + |
| 123 | + // Create agent with tools |
| 124 | + let mut agent = Agent::new( |
| 125 | + AgentRuntimeConfig::default(), |
| 126 | + "You are a helpful assistant with access to weather data.", |
| 127 | + None, |
| 128 | + tools, |
| 129 | + ); |
| 130 | + |
| 131 | + agent.send_request("What's the weather in Paris?", RequestMode::Normal); |
| 132 | + |
| 133 | + loop { |
| 134 | + match agent.next().await { |
| 135 | + Some(AgentStep::TextDelta(text)) => print!("{}", text), |
| 136 | + |
| 137 | + Some(AgentStep::ToolRequest(calls)) => { |
| 138 | + // Handle each tool call |
| 139 | + for call in calls { |
| 140 | + let result = execute_tool(&call); |
| 141 | + agent.submit_tool_result(&call.call_id, result); |
| 142 | + } |
| 143 | + // Continue processing after submitting results |
| 144 | + } |
| 145 | + |
| 146 | + Some(AgentStep::Finished { .. }) => { |
| 147 | + println!(); |
| 148 | + break; |
| 149 | + } |
| 150 | + |
| 151 | + Some(AgentStep::Error(e)) => { |
| 152 | + eprintln!("Error: {}", e); |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + None => break, |
| 157 | + _ => {} |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +fn execute_tool(call: &ToolCall) -> String { |
| 163 | + match call.name.as_str() { |
| 164 | + "get_weather" => { |
| 165 | + let location = call.params["location"].as_str().unwrap_or("unknown"); |
| 166 | + // Your actual implementation here |
| 167 | + format!("Weather in {}: Sunny, 22°C", location) |
| 168 | + } |
| 169 | + _ => format!("Unknown tool: {}", call.name), |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### `ToolCall` Structure |
| 175 | + |
| 176 | +When you receive a tool request, each `ToolCall` contains: |
| 177 | + |
| 178 | +```rust |
| 179 | +pub struct ToolCall { |
| 180 | + pub call_id: String, // Unique ID for this call (use with submit_tool_result) |
| 181 | + pub name: String, // Tool name |
| 182 | + pub params: serde_json::Value, // Parameters from the LLM |
| 183 | + // ... other fields |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +## Public API Reference |
| 188 | + |
| 189 | +### `Agent` |
| 190 | + |
| 191 | +The main agent type for conversations with Claude. |
| 192 | + |
| 193 | +```rust |
| 194 | +let mut agent = Agent::new(config, system_prompt, oauth, tools); |
| 195 | +agent.send_request("Hello!", RequestMode::Normal); |
| 196 | +while let Some(step) = agent.next().await { /* ... */ } |
| 197 | +agent.submit_tool_result(&call_id, result); |
| 198 | +``` |
| 199 | + |
| 200 | +### `AgentRuntimeConfig` |
| 201 | + |
| 202 | +```rust |
| 203 | +let config = AgentRuntimeConfig { |
| 204 | + model: "claude-sonnet-4-20250514".to_string(), |
| 205 | + max_tokens: 8192, |
| 206 | + thinking_budget: 2_000, |
| 207 | + max_retries: 5, |
| 208 | + compaction_thinking_budget: 8_000, |
| 209 | +}; |
| 210 | + |
| 211 | +// Or use defaults |
| 212 | +let config = AgentRuntimeConfig::default(); |
| 213 | +``` |
| 214 | + |
| 215 | +### `AgentStep` |
| 216 | + |
| 217 | +Events emitted during processing: |
| 218 | + |
| 219 | +- `TextDelta(String)` - Streaming text output |
| 220 | +- `ThinkingDelta(String)` - Extended thinking output |
| 221 | +- `ToolRequest(Vec<ToolCall>)` - LLM wants to use tools |
| 222 | +- `Finished { usage: Usage }` - Processing complete |
| 223 | +- `Error(String)` - Error occurred |
| 224 | +- `Retrying { attempt, error }` - Retrying after error |
| 225 | + |
| 226 | +### `SimpleTool` |
| 227 | + |
| 228 | +Define a tool for the LLM to use: |
| 229 | + |
| 230 | +```rust |
| 231 | +let tool = SimpleTool::new( |
| 232 | + "tool_name", // Name the LLM will use |
| 233 | + "Description of tool", // Help the LLM understand when to use it |
| 234 | + json!({ /* JSON Schema for parameters */ }), |
| 235 | +); |
| 236 | +``` |
| 237 | + |
| 238 | +### `ToolRegistry` |
| 239 | + |
| 240 | +Manage available tools: |
| 241 | + |
| 242 | +```rust |
| 243 | +let mut tools = ToolRegistry::empty(); |
| 244 | +tools.register(Arc::new(my_tool)); |
| 245 | +``` |
| 246 | + |
| 247 | +### `Usage` |
| 248 | + |
| 249 | +Token usage statistics: |
| 250 | + |
| 251 | +```rust |
| 252 | +pub struct Usage { |
| 253 | + pub output_tokens: u32, |
| 254 | + pub context_tokens: u32, |
| 255 | + pub cache_creation_tokens: u32, |
| 256 | + pub cache_read_tokens: u32, |
| 257 | +} |
| 258 | +``` |
| 259 | + |
| 260 | +## Authentication |
| 261 | + |
| 262 | +Set the `ANTHROPIC_API_KEY` environment variable: |
| 263 | + |
| 264 | +```bash |
| 265 | +export ANTHROPIC_API_KEY=sk-ant-... |
| 266 | +``` |
0 commit comments