Back to Eliza

@elizaos/plugin-anthropic

plugins/plugin-anthropic/README.md

2.0.16.4 KB
Original Source

@elizaos/plugin-anthropic

Anthropic Claude API client for elizaOS, providing text generation, streaming, image description, and structured JSON object generation capabilities.

Features

  • ๐Ÿš€ Text Generation - Generate text with Claude models (small/large), including streaming
  • ๐Ÿ–ผ๏ธ Image Description - Generate titles and descriptions from image URLs
  • ๐Ÿ“‹ 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

Quick Start

TypeScript

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 via TEXT_LARGE with a responseSchema (native tool calling)
const result = await runtime.useModel(ModelType.TEXT_LARGE, {
  prompt: "Create a user profile with name, email, and age",
  responseSchema: { type: "object" },
});

Installation

TypeScript (npm)

bash
npm install @elizaos/plugin-anthropic
# or
bun add @elizaos/plugin-anthropic

Configuration

All implementations use the same environment variables:

VariableRequiredDefaultDescription
ANTHROPIC_API_KEYYes-Your Anthropic API key
ANTHROPIC_BASE_URLNohttps://api.anthropic.comAPI base URL
ANTHROPIC_SMALL_MODELNoclaude-haiku-4-5-20251001Small model ID
ANTHROPIC_LARGE_MODELNoclaude-sonnet-4-6Large model ID
ANTHROPIC_TIMEOUT_SECONDSNo60Request timeout
ANTHROPIC_EXPERIMENTAL_TELEMETRYNofalseEnable telemetry (TS only)
ANTHROPIC_COT_BUDGETNo0Chain-of-thought token budget (TS only)

Available Models

Model IDSizeDescription
claude-haiku-4-5-20251001SmallFastest current Claude model
claude-sonnet-4-6LargeDefault large model
claude-opus-4-7LargeMost capable current model

API Reference

Model Types

  • TEXT_SMALL - Text generation with small model (supports tools, toolChoice, responseSchema for structured output via native tool calling)
  • TEXT_LARGE - Text generation with large model (supports tools, toolChoice, responseSchema for structured output via native tool calling)
  • IMAGE_DESCRIPTION - Image analysis with title and description output

Text Generation Parameters

ParameterTypeDescription
promptstringThe prompt to generate from
messagesarray?Multi-turn message history (supersedes prompt when both supplied)
systemstring?Optional system prompt
maxTokensnumber?Maximum tokens to generate
temperaturenumber?Randomness (0-1, can't use with topP)
topPnumber?Nucleus sampling (can't use with temperature)
stopSequencesstring[]?Stop generation at these sequences
streamboolean?Return a streaming text result when true
toolsToolSet?Native Anthropic tool definitions for tool calling
toolChoiceobject?Tool selection hint (auto, required, none, or named tool)
responseSchemaobject?JSON Schema for structured output (routes through native tool calling)

Project Structure

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

Development

Prerequisites

  • TypeScript: Bun or Node.js 18+
  • Rust: Rust 1.70+ with cargo
  • Python: Python 3.11+

Running Tests

bash
cd typescript
npx vitest

# With integration tests (requires API key)
ANTHROPIC_API_KEY=your-key npx vitest

Building

bash
# TypeScript
bun run build