Skip to content
Learni
View all tutorials
Intelligence Artificielle

How to Integrate Claude 3.5 Sonnet in Production in 2026

Lire en français

Introduction

Claude 3.5 Sonnet sets the current standard for expert AI applications in 2026. This tutorial walks you through advanced integration of its API inside a Next.js architecture. You will learn how to leverage native tools, structured streaming, and fine-grained token management. The goal is to build a reliable, scalable, and secure system suitable for production environments with monitoring and rate limiting.

Prerequisites

  • Node.js 20+ and Next.js 15
  • Anthropic account with API key
  • Strong knowledge of TypeScript and REST APIs
  • Docker for deployment

Project Initialization

terminal
npx create-next-app@latest claude-app --yes
cd claude-app
npm install @anthropic-ai/sdk zod

Initializes a Next.js 15 project and installs the official Anthropic SDK along with Zod for strict tool schema validation.

Environment Variables Configuration

.env.local
ANTHROPIC_API_KEY=sk-ant-api03-xxxxx
ANTHROPIC_MODEL=claude-3-5-sonnet-20241022
MAX_TOKENS=4096

Stores the API key and critical parameters. Never commit this file and use a secrets manager in production.

Typed Anthropic Client

lib/anthropic.ts
import Anthropic from '@anthropic-ai/sdk';

export const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY!,
  maxRetries: 3,
  timeout: 60000,
});

Creates a singleton client with configured retries and timeout for production. Prevents connection leaks.

Structured Tool Definition

lib/tools.ts
import { z } from 'zod';

export const searchTool = {
  name: 'web_search',
  description: 'Real-time web search',
  input_schema: z.object({
    query: z.string().min(3),
    max_results: z.number().max(10).default(5)
  })
};

Uses Zod to define validated tools. This ensures Claude respects the expected schema and prevents parameter hallucinations.

API Call with Tools and Streaming

app/api/claude/route.ts
import { anthropic } from '@/lib/anthropic';
import { searchTool } from '@/lib/tools';

export async function POST(req: Request) {
  const { messages } = await req.json();
  const stream = await anthropic.messages.create({
    model: process.env.ANTHROPIC_MODEL!,
    max_tokens: 4096,
    messages,
    tools: [searchTool],
    stream: true,
  });
  return new Response(stream);
}

Implements a streaming endpoint with tools. Streaming enables low perceived latency and real-time tool call handling.

Best Practices

  • Always validate inputs with Zod before sending to Claude
  • Implement per-user rate limiting with Upstash or Redis
  • Log tokens used to control costs
  • Use versioned and tested system prompts
  • Set up a circuit breaker for API calls

Common Errors to Avoid

  • Forgetting to handle tool_use blocks in streaming
  • Not limiting the number of tool call rounds (risk of infinite loops)
  • Exposing the API key on the client side
  • Ignoring context limits and 429 errors

Going Further

Deepen your knowledge of multi-agent orchestration and semantic caching in our Learni trainings.