Skip to main content
Anthropic provides Claude models with excellent document understanding and structured output capabilities.

Installation

npm install @docloai/providers-llm

Basic Setup

import { createVLMProvider } from '@docloai/providers-llm';

const provider = createVLMProvider({
  provider: 'anthropic',
  model: 'anthropic/claude-sonnet-4.5',
  apiKey: process.env.OPENROUTER_API_KEY!,
  via: 'openrouter'
});

Native API

const provider = createVLMProvider({
  provider: 'anthropic',
  model: 'claude-sonnet-4.5',
  apiKey: process.env.ANTHROPIC_API_KEY!
});

Available Models

ModelContextReasoningBest For
claude-opus-4.5200kYesComplex extraction
claude-sonnet-4.5200kYesBalanced performance
claude-haiku-4.5200kYesFast, cost-effective
claude-opus-4200kYesPrevious generation
claude-sonnet-4200kYesPrevious generation

OpenRouter Model IDs

When using OpenRouter, prefix models with anthropic/:
// OpenRouter
model: 'anthropic/claude-sonnet-4.5'

// Native
model: 'claude-sonnet-4.5'

Configuration Options

createVLMProvider({
  provider: 'anthropic',
  model: string,           // Model ID
  apiKey: string,          // API key
  via?: 'openrouter',      // Access method
  baseUrl?: string,        // Custom endpoint
  limits?: {
    maxFileSize?: number,      // Max file size (bytes)
    requestTimeout?: number,   // Timeout (ms)
    maxJsonDepth?: number      // Max JSON nesting
  }
})

Capabilities

FeatureSupport
ImagesYes (PNG, JPEG, WebP, GIF)
PDFsYes (up to 100 pages)
Structured OutputYes (tool calling / output_format)
ReasoningYes (extended thinking)
StreamingYes

Input Formats

Images

Claude requires base64-encoded images (URLs are downloaded automatically):
// Via Base64 (preferred)
{
  images: [{
    base64: 'data:image/jpeg;base64,...',
    mimeType: 'image/jpeg'
  }]
}
Supported formats: PNG, JPEG, WebP, GIF (max 5MB per image, 8000x8000 max).

PDFs

// Via Base64
{
  pdfs: [{
    base64: 'data:application/pdf;base64,...'
  }]
}

// Via Files API (native only)
{
  pdfs: [{
    fileId: 'file_abc123'
  }]
}
PDF limits: 32MB per file, 100 pages for full visual analysis.

Extended Thinking

Claude models support extended thinking for complex reasoning:
import { createFlow, extract } from '@docloai/flows';

const flow = createFlow()
  .step('extract', extract({
    provider: createVLMProvider({
      provider: 'anthropic',
      model: 'anthropic/claude-sonnet-4.5',
      apiKey: process.env.OPENROUTER_API_KEY!,
      via: 'openrouter'
    }),
    schema: complexSchema,
    reasoning: {
      enabled: true,
      effort: 'high'
    }
  }))
  .build();
Reasoning is configured via max_tokens (OpenRouter) or budget_tokens (native):
EffortOpenRouterNative
low20% of max_tokens1024 tokens min
medium50% of max_tokensCalculated
high80% of max_tokensUp to 32000 tokens

Prompt Caching

Claude supports prompt caching for repeated extractions with the same schema:
// Caching is automatic when using OpenRouter
// Add cache_control to text blocks for optimal caching
Cache metrics are returned in the response:
  • cacheCreationInputTokens: Tokens written to cache
  • cacheReadInputTokens: Tokens read from cache

Structured Output

Claude 4.5 models support native structured output via output_format. Older models use tool calling:
// Automatically handled - SDK detects model version
const provider = createVLMProvider({
  provider: 'anthropic',
  model: 'anthropic/claude-sonnet-4.5',
  apiKey: process.env.OPENROUTER_API_KEY!,
  via: 'openrouter'
});
Model support:
  • Sonnet 4.5+: Native output_format with json_schema
  • Opus 4.1+: Native output_format with json_schema
  • Haiku: Tool calling fallback

Production Setup

import { buildLLMProvider } from '@docloai/providers-llm';

const provider = buildLLMProvider({
  providers: [
    {
      provider: 'anthropic',
      model: 'anthropic/claude-sonnet-4.5',
      apiKey: process.env.OPENROUTER_API_KEY!,
      via: 'openrouter'
    },
    {
      provider: 'anthropic',
      model: 'anthropic/claude-haiku-4.5',
      apiKey: process.env.OPENROUTER_API_KEY!,
      via: 'openrouter'
    }
  ],
  maxRetries: 2,
  retryDelay: 1000,
  useExponentialBackoff: true
});

Pricing

Via OpenRouter (approximate):
ModelInput (per 1k)Output (per 1k)
claude-opus-4.5$0.015$0.075
claude-sonnet-4.5$0.003$0.015
claude-haiku-4.5$0.0008$0.004

Example: Contract Analysis

import { createFlow, extract } from '@docloai/flows';
import { createVLMProvider } from '@docloai/providers-llm';

const provider = createVLMProvider({
  provider: 'anthropic',
  model: 'anthropic/claude-sonnet-4.5',
  apiKey: process.env.OPENROUTER_API_KEY!,
  via: 'openrouter'
});

const contractSchema = {
  type: 'object',
  properties: {
    parties: {
      type: 'array',
      items: { type: 'string' }
    },
    effectiveDate: { type: 'string' },
    terminationDate: { type: 'string' },
    keyTerms: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          term: { type: 'string' },
          description: { type: 'string' }
        }
      }
    }
  }
};

const flow = createFlow()
  .step('extract', extract({
    provider,
    schema: contractSchema,
    reasoning: { enabled: true, effort: 'medium' }
  }))
  .build();

const result = await flow.run({
  base64: 'data:application/pdf;base64,...'
});

Next Steps