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>
  );
}

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