Anthropic AI Provider (@voltagent/anthropic-ai
)
The Anthropic AI Provider integrates VoltAgent with Anthropic's Claude models. It wraps the official @anthropic-ai/sdk
SDK.
Key Characteristics:
- Claude Models: Supports all Claude models through the Anthropic API.
- Model Agnostic: Accepts standard Claude model identifier strings (e.g.,
'claude-3-sonnet-20240229'
,'claude-3-opus-20240229'
). - Core Functionality: Focuses on text generation, streaming, and structured object generation using the underlying Anthropic SDK.
Installation​
- npm
- yarn
- pnpm
npm install @voltagent/core @voltagent/anthropic-ai zod
yarn add @voltagent/core @voltagent/anthropic-ai zod
pnpm add @voltagent/core @voltagent/anthropic-ai zod
Note: @anthropic-ai/sdk
is a peer dependency. zod
is required if using generateObject
.
Configuration​
The AnthropicProvider
requires an API key, which can be provided directly or through an environment variable.
import { AnthropicProvider } from "@voltagent/anthropic-ai";
// Option 1: Direct API Key
const anthropicProvider = new AnthropicProvider({
apiKey: "YOUR_ANTHROPIC_API_KEY",
});
// Option 2: Environment Variable (ANTHROPIC_API_KEY)
// Ensure process.env.ANTHROPIC_API_KEY is set
const anthropicProvider = new AnthropicProvider({});
Usage​
Instantiate your Agent
with the configured AnthropicProvider
:
import { Agent } from "@voltagent/core";
import { AnthropicProvider } from "@voltagent/anthropic-ai";
const anthropicProvider = new AnthropicProvider({ apiKey: "YOUR_ANTHROPIC_API_KEY" });
const agent = new Agent({
name: "Claude Agent",
instructions: "An agent powered by Claude",
llm: anthropicProvider,
model: "claude-3-sonnet-20240229", // Specify the desired Claude model
});
// Example call
async function run() {
const response = await agent.generateText("What is the capital of France?");
console.log(response.text);
}
run();
Supported Methods​
generateText
: ✅ Supported. Calls Anthropic SDK'smessages.create
.streamText
: ✅ Supported. Calls Anthropic SDK'smessages.create
withstream: true
.generateObject
: ✅ Supported. Uses Anthropic's structured output capabilities with Zod schema validation.streamObject
: ✅ Supported. Streams structured objects with schema validation.
Tool Calling Support​
✅ Supported.
The @voltagent/anthropic-ai
provider supports tool calling through the MCP (Model Control Protocol) format:
import { createTool } from "@voltagent/core";
import { z } from "zod";
const weatherTool = createTool({
name: "get_current_weather",
description: "Get the current weather in a location",
parameters: z.object({
location: z.string().describe("The location to get weather for"),
}),
execute: async (input) => {
return {
location: input.location,
temperature: 72,
condition: "sunny",
};
},
});
const agent = new Agent({
name: "weather-agent",
instructions: "A helpful weather assistant",
llm: new AnthropicProvider(),
model: "claude-3-sonnet-20240229",
tools: [weatherTool],
});
Model Selection & Options​
The specific Claude model (e.g., 'claude-3-sonnet-20240229'
, 'claude-3-opus-20240229'
) is set via the model
property during Agent
instantiation.
You can override or provide additional Anthropic-specific generation parameters (like temperature
, max_tokens
, top_p
, stop_sequences
, etc.) per-request using the provider
key within the options object of generateText
, streamText
, or generateObject
.
// Example: Overriding temperature for a specific call
const response = await agent.generateText("Write a creative story.", {
provider: {
temperature: 0.9,
max_tokens: 1024,
// Any other valid Anthropic parameters
},
});
Refer to the Anthropic API documentation for the full list of available configuration parameters.