@anvia/anthropic
Anthropic completion models for Anvia.
@anvia/anthropic adapts the Anthropic SDK into Anvia's completion model interface. It supports streaming, tool use, and the full Anthropic messages API.
Install
pnpm add @anvia/anthropicThe Anthropic SDK (@anthropic-ai/sdk) is a transitive dependency.
Quick Start
import { AgentBuilder } from "@anvia/core";
import { AnthropicClient } from "@anvia/anthropic";
const client = new AnthropicClient({ apiKey });
const model = client.completionModel("claude-opus-4-6");
const agent = new AgentBuilder("support", model)
.instructions("Answer clearly and concisely.")
.build();
const response = await agent.prompt("Explain the agent runtime.").send();
console.log(response.output);Client Configuration
type AnthropicClientOptions = {
apiKey?: string; // required unless `client` is provided
baseUrl?: string; // custom endpoint
client?: Anthropic; // pre-configured Anthropic SDK instance
};Provider clients only use explicit constructor values. They do not read environment variables.
// Standard
const client = new AnthropicClient({ apiKey });
// Custom base URL (e.g., proxy)
const client = new AnthropicClient({
apiKey,
baseUrl: "https://my-proxy.com",
});
// Pre-configured SDK instance
const client = new AnthropicClient({ client: existingAnthropicInstance });Completion Models
// Default model: claude-sonnet-4-20250514
const model = client.completionModel();
// Specific model
const model = client.completionModel("claude-opus-4-6");The model implements StreamingCompletionModel:
// Non-streaming
const response = await model.completion(request);
// Streaming
for await (const event of model.streamCompletion(request)) {
console.log(event);
}Model Listing
const models = await client.listModels();
console.log(models.data.map((m) => m.id));Fetches available Anthropic models and returns a normalized ModelList.
Streaming with Agents
Anthropic streaming works transparently with AgentBuilder:
const agent = new AgentBuilder("writer", model)
.instructions("Write concise technical summaries.")
.build();
// Stream events
for await (const event of agent.prompt("Summarize the runtime.").stream()) {
if (event.type === "text_delta") {
process.stdout.write(event.text);
}
}Tool Use
Anthropic models support tool use through the standard Anvia tool system:
import { createTool } from "@anvia/core";
import { z } from "zod";
const search = createTool({
name: "search_docs",
description: "Search documentation.",
input: z.object({ query: z.string() }),
output: z.object({ results: z.array(z.string()) }),
async execute({ query }) {
return { results: [`Result for: ${query}`] };
},
});
const agent = new AgentBuilder("support", model)
.tool(search)
.defaultMaxTurns(3)
.build();Error Handling
- Constructor throws when neither
clientnorapiKeyis supplied listModels()throwsModelListingErrorwhen the provider request fails- Model operations throw Anthropic SDK errors directly
Related
- Models for model name conventions
- Anthropic Reference for full API types
- Getting Started for a first agent walkthrough
