Anthropic

The Messages API with adaptive thinking, tool use, vision, and prompt caching.

let model = AnthropicModel("claude-fable-5")     // or claude-sonnet-5

Key from ANTHROPIC_API_KEY; base URL https://api.anthropic.com/v1.

Features

  • Tools, vision, and PDF documents as message parts.
  • Structured output rides a forced tool call — Claude has no JSON mode, so generateObject defines a tool whose arguments are your schema and forces the model to call it.
  • Thinking streams as .reasoningDelta; the reasoning parameter translates per model tier (below).
  • usage.cachedInputTokens is populated on prompt-cache hits; result.providerMetadata["anthropic"]["cacheCreationInputTokens"] reports tokens written to the cache.
  • Anthropic-native fields (thinking, cache-control blocks) go through providerOptions and win over anything the library sets.

Server-side tools

Anthropic's built-in tools have typed builders under AnthropicModel.ToolswebSearch, webFetch, codeExecution, bash, textEditor, computer(displayWidthPx:displayHeightPx:), and memory. Pass them in tools:; the required anthropic-beta flags are added to the request for you:

let result = try await generateText(
  model: AnthropicModel("claude-sonnet-5"),
  prompt: "Search the web and summarize what's new.",
  tools: [AnthropicModel.Tools.webSearch(maxUses: 3)]
)

Each builder takes a version: for the dated tool variants (web_search_20250305, computer_20250124, ...); the defaults track the current stable release. Newer versions are wired too, with their beta headers: web_search_20260318, web_fetch_20260318 / 20260309, code_execution_20260521 / 20260120 / 20250825, computer_20251124, plus advisor, toolSearchBm25 / toolSearchRegex, and mcpToolset for the MCP connector. The three current code-execution versions are GA and need no beta header; code_execution_20250522 is the legacy Python-only build and is no longer the default.

Tool definition properties

Anthropic accepts optional properties on any tool — built-in or your own. Tool.loading(_:) carries them:

let deleteFile = Tool(name: "deleteFile", description: , parameters: ) {  }
  .loading(ToolLoading(
    strict: true,                                  // validate names and inputs
    deferLoading: true,                            // keep out of the cached prompt
    allowedCallers: ["code_execution_20260120"],   // sandbox-only
    eagerInputStreaming: true                      // fine-grained input streaming
  ))

.ephemeralCache() sets a cache_control breakpoint at the tool definition, and .codeExecutionOnly() is shorthand for the sandbox-only caller list. It defaults to code_execution_20260120, the caller Anthropic documents for programmatic tool calling — the same value web_search_20260209 and later default to. That is deliberately not the version Tools.codeExecution() sends by default (code_execution_20260521): if you want the model to call your tool from inside the sandbox, declare Tools.codeExecution(version: "code_execution_20260120") so the caller matches. Pass codeExecutionOnly(version:) to pin a different one. defer_loading keeps a tool out of the cached system-prompt prefix until tool search surfaces it, so adding deferred tools does not invalidate an existing prompt cache.

A tool's inputExamples are sent natively as input_examples here, so the description stays clean — the addToolInputExamples middleware is only needed for providers with no native field.

Platform APIs

  • AnthropicBatchClient — Message Batches: create, list, get, results (streamed JSONL, decoded per line), cancel, delete.
  • AnthropicModelsClientlist and get for the model catalog.
  • AnthropicModel.countTokens(_:tools:system:) — pre-flight sizing against /v1/messages/count_tokens, with the same beta headers your tools require.

Models

ModelsOutput ceilingreasoning becomes
Sonnet 5, Fable 5, Opus 4.7 / 4.8128kAdaptive thinking + output_config.effort, up to xhigh
Sonnet 4.6, Opus 4.6128kAdaptive thinking (xhigh coerces to max)
Sonnet 4.5, Opus 4.5, Haiku 4.5, Sonnet 4.x64kthinking.budget_tokens from the ceiling
Opus 4.1, Opus 4.x32kthinking.budget_tokens from the ceiling
Anything else4,096thinking.budget_tokens, conservative ceiling

.none sends thinking.type: "disabled" explicitly, and max_tokens is raised automatically when a budget would not fit.

Current lineup

Snapshot from July 2026, newest first. Anthropic's native ids use dashes (claude-opus-4-8); date-stamped variants (claude-sonnet-4-5-20250929) work too.

ModelReleased
claude-fable-5July 2026
claude-sonnet-5June 2026
claude-opus-4-8May 2026
claude-opus-4-7April 2026
claude-sonnet-4-6, claude-opus-4-6February 2026
claude-opus-4-5November 2025
claude-haiku-4-5October 2025
claude-sonnet-4-5September 2025
claude-opus-4-1, claude-sonnet-42025
claude-3-5-haikuBudget tier

Files and skills

These resource clients are documented in Files and skills.

let upload = try await AnthropicFiles().upload(
  data: pdf, filename: "report.pdf", mediaType: "application/pdf"
)

let skill = try await AnthropicSkills().upload(
  files: [SkillFile(path: "brand-guide/SKILL.md", data: skillMD)],
  displayTitle: "Brand guide"
)

Beta headers are handled for you on both.