Anthropic

Use Claude 3.5 Sonnet, Claude 3 Opus, and more

Anthropic

Anthropic

Claude 3.5 Sonnet, Claude 3 Opus

Claude models from Anthropic. Known for safety, long context, and excellent reasoning.


Setup

1. Install Packages

npm install @yourgpt/copilot-sdk @yourgpt/llm-sdk @anthropic-ai/sdk

2. Get API Key

Get your API key from console.anthropic.com

3. Add Environment Variable

.env.local
ANTHROPIC_API_KEY=sk-ant-...

4. Usage

import { generateText } from '@yourgpt/llm-sdk';
import { anthropic } from '@yourgpt/llm-sdk/anthropic';

const result = await generateText({
  model: anthropic('claude-sonnet-4-20250514'),
  prompt: 'Explain the theory of relativity.',
});

console.log(result.text);

5. Streaming (API Route)

app/api/chat/route.ts
import { streamText } from '@yourgpt/llm-sdk';
import { anthropic } from '@yourgpt/llm-sdk/anthropic';

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = await streamText({
    model: anthropic('claude-sonnet-4-20250514'),
    system: 'You are a helpful assistant.',
    messages,
  });

  return result.toTextStreamResponse();
}

Available Models

// Claude 4 (Latest)
anthropic('claude-sonnet-4-20250514')    // Best balance of speed & quality
anthropic('claude-opus-4-20250514')      // Most capable

// Claude 3.7
anthropic('claude-3-7-sonnet-20250219')  // Extended thinking support
anthropic('claude-3-7-sonnet-latest')    // Latest 3.7 version

// Claude 3.5
anthropic('claude-sonnet-4-20250514')  // Great performance
anthropic('claude-3-5-sonnet-latest')    // Latest 3.5 version
anthropic('claude-3-5-haiku-20241022')   // Fast and affordable

// Claude 3
anthropic('claude-3-opus-20240229')      // High quality
anthropic('claude-3-sonnet-20240229')    // Balanced
anthropic('claude-3-haiku-20240307')     // Fastest

Configuration Options

import { anthropic } from '@yourgpt/llm-sdk/anthropic';

// Custom API key
const model = anthropic('claude-3-5-sonnet-20241022', {
  apiKey: 'sk-ant-custom-key',
});

// With generation options
const result = await generateText({
  model: anthropic('claude-sonnet-4-20250514'),
  prompt: 'Hello',
  temperature: 0.7,        // 0-1
  maxTokens: 4096,         // Max response length
});

Tool Calling

Claude has robust tool support:

import { generateText, tool } from '@yourgpt/llm-sdk';
import { anthropic } from '@yourgpt/llm-sdk/anthropic';
import { z } from 'zod';

const result = await generateText({
  model: anthropic('claude-sonnet-4-20250514'),
  prompt: 'Analyze this document for key insights',
  tools: {
    analyzeDocument: tool({
      description: 'Analyze a document',
      parameters: z.object({
        documentId: z.string(),
        analysisType: z.enum(['summary', 'sentiment', 'entities']),
      }),
      execute: async ({ documentId, analysisType }) => {
        return await analyzeDocument(documentId, analysisType);
      },
    }),
  },
  maxSteps: 5,
});

Long Context

Claude excels at long documents (200K context):

const result = await generateText({
  model: anthropic('claude-sonnet-4-20250514'),
  system: `You are a support agent. Here is our full documentation:

  ${fullDocumentation}  // Can be 100K+ tokens

  Answer questions based on this documentation.`,
  prompt: userQuestion,
});

Vision (Image Input)

Claude models support image analysis:

const result = await generateText({
  model: anthropic('claude-sonnet-4-20250514'),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Analyze this screenshot' },
        { type: 'image', image: base64ImageData },
      ],
    },
  ],
});

With Copilot UI

Use with the Copilot React components:

app/providers.tsx
'use client';

import { CopilotProvider } from '@yourgpt/copilot-sdk/react';

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <CopilotProvider runtimeUrl="/api/chat">
      {children}
    </CopilotProvider>
  );
}

MCP servers

Anthropic supports remote Model Context Protocol servers natively via the Messages API. Pass mcpServers on the request config and the SDK routes through client.beta.messages.* with the required beta header.

const result = await runtime.response({
  prompt: 'Create a GitHub issue summarizing the deploy failures.',
  mcpServers: [{
    label: 'github',
    url: 'https://mcp.github.com/sse',
    headers: { Authorization: `Bearer ${process.env.GH_TOKEN}` },
    allowedTools: ['create_issue', 'search_issues'],
  }],
});

Beta header attached automatically: anthropic-beta: mcp-client-2025-11-20.

The shape sent to Anthropic uses the 2025-11-20 two-array form:

  • mcp_servers: [{ type: "url", url, name, authorization_token }] — connection entries (the SDK strips Bearer from the Authorization header value, since Anthropic expects the bare token).
  • tools: [{ type: "mcp_toolset", mcp_server_name, configs: {...} }] — tool filtering (only emitted when allowedTools is set).

See Structured Output → MCP servers for the cross-provider reference.


Extended thinking

Two ways to enable Claude's extended thinking:

Per-request (recommended) — pass reasoningEffort on the call. The SDK picks the right shape for the model:

const result = await runtime.response({
  prompt: 'Plan a multi-step refactor of the auth flow.',
  reasoningEffort: 'high',
});
  • Claude 4.6 / 4.7 (Opus 4.7, Sonnet 4.6, Opus 4.6): adaptive thinking — thinking: { type: 'adaptive', effort: 'high' }. The model chooses budget dynamically based on effort.
  • Claude 3.7 / 4.0 / 4.5 series: explicit budget — thinking: { type: 'enabled', budget_tokens: 16000 }. Buckets: low=4k, medium=8k, high=16k. Pass { budgetTokens: N } for an exact number.

budget_tokens is deprecated on Claude 4.6 and Sonnet 4.6. Use the reasoningEffort enum and the SDK picks adaptive mode automatically on those models.

Per-adapter (legacy) — set thinking on createAnthropic() config. Per-request reasoningEffort overrides the adapter default when both are set.

const anthropic = createAnthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
  thinking: { type: 'enabled', budgetTokens: 8000 },
});

Pricing

ModelInputOutput
claude-3-5-sonnet$3/1M tokens$15/1M tokens
claude-3-opus$15/1M tokens$75/1M tokens
claude-3-5-haiku$0.80/1M tokens$4/1M tokens

Check Anthropic pricing for current rates.


Next Steps

On this page