A multi-language Anthropic Claude API client for elizaOS, providing text generation and structured JSON object generation capabilities.
npm install @elizaos/plugin-anthropic-rootA multi-language Anthropic Claude API client for elizaOS, providing text generation and structured JSON object generation capabilities.
This plugin is implemented in three languages for maximum flexibility:
| Language | Package | Registry |
| ---------- | --------------------------- | --------- |
| TypeScript | @elizaos/plugin-anthropic | npm |
| Rust | elizaos-plugin-anthropic | crates.io |
| Python | elizaos-plugin-anthropic | PyPI |
All implementations share the same API design and behavior.
- ๐ Text Generation - Generate text with Claude models (small/large)
- ๐ Object Generation - Generate structured JSON objects with validation
- ๐ Strong Types - No any or unknown types, full type safety
- โก Fail Fast - Immediate errors on invalid input, no silent failures
- ๐งช Real Integration Tests - Tests against live Anthropic API
``typescript
import { anthropicPlugin } from "@elizaos/plugin-anthropic";
import { AgentRuntime, ModelType } from "@elizaos/core";
// Register the plugin
const runtime = new AgentRuntime({
plugins: [anthropicPlugin],
});
// Generate text
const text = await runtime.useModel(ModelType.TEXT_LARGE, {
prompt: "Explain quantum computing in simple terms",
});
// Generate JSON object
const result = await runtime.useModel(ModelType.OBJECT_SMALL, {
prompt: "Create a user profile with name, email, and age",
schema: { type: "object" },
});
`
`rust
use elizaos_plugin_anthropic::{AnthropicClient, AnthropicConfig, TextGenerationParams};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = AnthropicConfig::from_env()?;
let client = AnthropicClient::new(config)?;
let params = TextGenerationParams::new("Explain quantum computing")
.with_max_tokens(1024)
.with_temperature(0.7);
let response = client.generate_text_large(params).await?;
println!("Response: {}", response.text);
Ok(())
}
`
`python
import asyncio
from elizaos_plugin_anthropic import AnthropicClient, AnthropicConfig
async def main():
config = AnthropicConfig.from_env()
async with AnthropicClient(config) as client:
response = await client.generate_text_large("Explain quantum computing")
print(f"Response: {response.text}")
asyncio.run(main())
`
`bash`
npm install @elizaos/plugin-anthropicor
bun add @elizaos/plugin-anthropic
`toml`
[dependencies]
elizaos-plugin-anthropic = "1.0"
`bash`
pip install elizaos-plugin-anthropic
All implementations use the same environment variables:
| Variable | Required | Default | Description |
| ---------------------------------- | -------- | --------------------------- | --------------------------------------- |
| ANTHROPIC_API_KEY | Yes | - | Your Anthropic API key |ANTHROPIC_BASE_URL
| | No | https://api.anthropic.com | API base URL |ANTHROPIC_SMALL_MODEL
| | No | claude-3-5-haiku-20241022 | Small model ID |ANTHROPIC_LARGE_MODEL
| | No | claude-sonnet-4-20250514 | Large model ID |ANTHROPIC_TIMEOUT_SECONDS
| | No | 60 | Request timeout |ANTHROPIC_EXPERIMENTAL_TELEMETRY
| | No | false | Enable telemetry (TS only) |ANTHROPIC_COT_BUDGET
| | No | 0 | Chain-of-thought token budget (TS only) |
| Model ID | Size | Description |
| ---------------------------- | ----- | -------------------- |
| claude-3-5-haiku-20241022 | Small | Fast and efficient |claude-sonnet-4-20250514
| | Large | Most capable |claude-3-5-sonnet-20241022
| | Large | Balanced performance |claude-3-opus-20240229
| | Large | Previous flagship |
- TEXT_SMALL - Text generation with small modelTEXT_LARGE
- - Text generation with large modelOBJECT_SMALL
- - JSON generation with small modelOBJECT_LARGE
- - JSON generation with large model
| Parameter | Type | Description |
| --------------- | --------- | --------------------------------------------- |
| prompt | string | The prompt to generate from |system
| | string? | Optional system prompt |maxTokens
| | number? | Maximum tokens to generate |temperature
| | number? | Randomness (0-1, can't use with topP) |topP
| | number? | Nucleus sampling (can't use with temperature) |stopSequences
| | string[]? | Stop generation at these sequences |
| Parameter | Type | Description |
| ------------- | ------- | ----------------------------------------------- |
| prompt | string | Description of the object to generate |schema
| | object? | Optional JSON schema |temperature
| | number? | Randomness (default: 0.2 for structured output) |
``
plugin-anthropic/
โโโ typescript/ # TypeScript implementation
โ โโโ index.ts # Main entry point
โ โโโ models/ # Model handlers
โ โโโ providers/ # Anthropic client factories
โ โโโ types/ # Type definitions
โ โโโ utils/ # Utilities (config, JSON parsing)
โ โโโ __tests__/ # Unit and integration tests
โโโ rust/ # Rust implementation
โ โโโ src/ # Source code
โ โ โโโ lib.rs # Library entry
โ โ โโโ client.rs # API client
โ โ โโโ config.rs # Configuration
โ โ โโโ models.rs # Model definitions
โ โ โโโ types.rs # Type definitions
โ โ โโโ error.rs # Error types
โ โโโ tests/ # Integration tests
โโโ python/ # Python implementation
โ โโโ elizaos_plugin_anthropic/
โ โ โโโ __init__.py # Package entry
โ โ โโโ client.py # API client
โ โ โโโ config.py # Configuration
โ โ โโโ models.py # Model definitions
โ โ โโโ types.py # Type definitions
โ โ โโโ errors.py # Error types
โ โโโ tests/ # Integration tests
โโโ package.json # npm package config
โโโ README.md # This file
- TypeScript: Bun or Node.js 18+
- Rust: Rust 1.70+ with cargo
- Python: Python 3.11+
`bashTypeScript
cd typescript
npx vitest
$3
`bash
TypeScript
bun run buildRust (native library)
cd rust && cargo build --releaseRust (WASM)
cd rust && wasm-pack build --target web --out-dir pkg/webPython (wheel)
cd python && pip install build && python -m build
`Error Handling
All implementations follow a fail-fast philosophy:
- No try-catch blocks that swallow errors
- No fallback modes or mock modes
- Immediate validation of all inputs
- Clear error messages with actionable information
$3
| Error | Description |
| ----------------------- | ---------------------------------- |
|
ApiKeyError | API key missing or invalid |
| ConfigError | Configuration problem |
| RateLimitError | Rate limit exceeded (retryable) |
| ApiError | API returned an error |
| JsonGenerationError | Failed to parse JSON from response |
| InvalidParameterError | Invalid parameter provided |
| NetworkError | Network connectivity issue |
| TimeoutError` | Request timed out |MIT - see LICENSE
See the elizaOS contributing guide.