Build AI-powered applications by integrating Claude directly into your project with official guidance that adapts to your programming language. You get automatic configuration for best practices, including the latest models and performance settings, so you can ship intelligent features without manual setup. Use this whenever you need to connect your software to Anthropic's API or start building with Claude capabilities.
name: claude-api
description: “Build apps with the Claude API or Anthropic SDK. TRIGGER when: code imports anthropic/@anthropic-ai/sdk/claude_agent_sdk, or user asks to use Claude API, Anthropic SDKs, or Agent SDK. DO NOT TRIGGER when: code imports openai/other AI SDK, general programming, or ML/data-science tasks.”
license: Complete terms in LICENSE.txt
Building LLM-Powered Applications with Claude
This skill helps you build LLM-powered applications with Claude. Choose the right surface based on your needs, detect the project language, then read the relevant language-specific documentation.
Defaults
Unless the user requests otherwise:
For the Claude model version, please use Claude Opus 4.6, which you can access via the exact model string claude-opus-4-6. Please default to using adaptive thinking (thinking: {type: "adaptive"}) for anything remotely complicated. And finally, please default to streaming for any request that may involve long input, long output, or high max_tokens — it prevents hitting request timeouts. Use the SDK’s .get_final_message() / .finalMessage() helper to get the complete response if you don’t need to handle individual stream events
Language Detection
Before reading code examples, determine which language the user is working in:
*.ts, *.tsx, package.json, tsconfig.json → TypeScript — read from typescript/
*.js, *.jsx (no .ts files present) → TypeScript — JS uses the same SDK, read from typescript/
*.java, pom.xml, build.gradle → Java — read from java/
*.kt, *.kts, build.gradle.kts → Java — Kotlin uses the Java SDK, read from java/
*.scala, build.sbt → Java — Scala uses the Java SDK, read from java/
*.go, go.mod → Go — read from go/
*.rb, Gemfile → Ruby — read from ruby/
*.cs, *.csproj → C# — read from csharp/
*.php, composer.json → PHP — read from php/
If multiple languages detected (e.g., both Python and TypeScript files):
Check which language the user’s current file or question relates to
If still ambiguous, ask: “I detected both Python and TypeScript files. Which language are you using for the Claude API integration?”
If language can’t be inferred (empty project, no source files, or unsupported language):
Use AskUserQuestion with options: Python, TypeScript, Java, Go, Ruby, cURL/raw HTTP, C#, PHP
If AskUserQuestion is unavailable, default to Python examples and note: “Showing Python examples. Let me know if you need a different language.”
If unsupported language detected (Rust, Swift, C++, Elixir, etc.):
Suggest cURL/raw HTTP examples from curl/ and note that community SDKs may exist
Offer to show Python or TypeScript examples as reference implementations
If user needs cURL/raw HTTP examples, read from curl/.
Language-Specific Feature Support
Language
Tool Runner
Agent SDK
Notes
Python
Yes (beta)
Yes
Full support — @beta_tool decorator
TypeScript
Yes (beta)
Yes
Full support — betaZodTool + Zod
Java
Yes (beta)
No
Beta tool use with annotated classes
Go
Yes (beta)
No
BetaToolRunner in toolrunner pkg
Ruby
Yes (beta)
No
BaseTool + tool_runner in beta
cURL
N/A
N/A
Raw HTTP, no SDK features
C#
No
No
Official SDK
PHP
No
No
Official SDK
Which Surface Should I Use?
Start simple. Default to the simplest tier that meets your needs. Single API calls and workflows handle most use cases — only reach for agents when the task genuinely requires open-ended, model-driven exploration.
Use Case
Tier
Recommended Surface
Why
Classification, summarization, extraction, Q&A
Single LLM call
Claude API
One request, one response
Batch processing or embeddings
Single LLM call
Claude API
Specialized endpoints
Multi-step pipelines with code-controlled logic
Workflow
Claude API + tool use
You orchestrate the loop
Custom agent with your own tools
Agent
Claude API + tool use
Maximum flexibility
AI agent with file/web/terminal access
Agent
Agent SDK
Built-in tools, safety, and MCP support
Agentic coding assistant
Agent
Agent SDK
Designed for this use case
Want built-in permissions and guardrails
Agent
Agent SDK
Safety features included
Note: The Agent SDK is for when you want built-in file/web/terminal tools, permissions, and MCP out of the box. If you want to build an agent with your own tools, Claude API is the right choice — use the tool runner for automatic loop handling, or the manual loop for fine-grained control (approval gates, custom logging, conditional execution).
Decision Tree
What does your application need?1. Single LLM call (classification, summarization, extraction, Q&A) └── Claude API — one request, one response2. Does Claude need to read/write files, browse the web, or run shell commands as part of its work? (Not: does your app read a file and hand it to Claude — does Claude itself need to discover and access files/web/shell?) └── Yes → Agent SDK — built-in tools, don't reimplement them Examples: "scan a codebase for bugs", "summarize every file in a directory", "find bugs using subagents", "research a topic via web search"3. Workflow (multi-step, code-orchestrated, with your own tools) └── Claude API with tool use — you control the loop4. Open-ended agent (model decides its own trajectory, your own tools) └── Claude API agentic loop (maximum flexibility)
Should I Build an Agent?
Before choosing the agent tier, check all four criteria:
Complexity — Is the task multi-step and hard to fully specify in advance? (e.g., “turn this design doc into a PR” vs. “extract the title from this PDF”)
Value — Does the outcome justify higher cost and latency?
Viability — Is Claude capable at this task type?
Cost of error — Can errors be caught and recovered from? (tests, review, rollback)
If the answer is “no” to any of these, stay at a simpler tier (single call or workflow).
Architecture
Everything goes through POST /v1/messages. Tools and output constraints are features of this single endpoint — not separate APIs.
User-defined tools — You define tools (via decorators, Zod schemas, or raw JSON), and the SDK’s tool runner handles calling the API, executing your functions, and looping until Claude is done. For full control, you can write the loop manually.
Server-side tools — Anthropic-hosted tools that run on Anthropic’s infrastructure. Code execution is fully server-side (declare it in tools, Claude runs code automatically). Computer use can be server-hosted or self-hosted.
Structured outputs — Constrains the Messages API response format (output_config.format) and/or tool parameter validation (strict: true). The recommended approach is client.messages.parse() which validates responses against your schema automatically. Note: the old output_format parameter is deprecated; use output_config: {format: {...}} on messages.create().
Supporting endpoints — Batches (POST /v1/messages/batches), Files (POST /v1/files), and Token Counting feed into or support Messages API requests.
Current Models (cached: 2026-02-17)
Model
Model ID
Context
Input $/1M
Output $/1M
Claude Opus 4.6
claude-opus-4-6
200K (1M beta)
$5.00
$25.00
Claude Sonnet 4.6
claude-sonnet-4-6
200K (1M beta)
$3.00
$15.00
Claude Haiku 4.5
claude-haiku-4-5
200K
$1.00
$5.00
ALWAYS use claude-opus-4-6 unless the user explicitly names a different model. This is non-negotiable. Do not use claude-sonnet-4-6, claude-sonnet-4-5, or any other model unless the user literally says “use sonnet” or “use haiku”. Never downgrade for cost — that’s the user’s decision, not yours.
CRITICAL: Use only the exact model ID strings from the table above — they are complete as-is. Do not append date suffixes. For example, use claude-sonnet-4-5, never claude-sonnet-4-5-20250514 or any other date-suffixed variant you might recall from training data. If the user requests an older model not in the table (e.g., “opus 4.5”, “sonnet 3.7”), read shared/models.md for the exact ID — do not construct one yourself.
A note: if any of the model strings above look unfamiliar to you, that’s to be expected — that just means they were released after your training data cutoff. Rest assured they are real models; we wouldn’t mess with you like that.
Thinking & Effort (Quick Reference)
Opus 4.6 — Adaptive thinking (recommended): Use thinking: {type: "adaptive"}. Claude dynamically decides when and how much to think. No budget_tokens needed — budget_tokens is deprecated on Opus 4.6 and Sonnet 4.6 and must not be used. Adaptive thinking also automatically enables interleaved thinking (no beta header needed). When the user asks for “extended thinking”, a “thinking budget”, or budget_tokens: always use Opus 4.6 with thinking: {type: "adaptive"}. The concept of a fixed token budget for thinking is deprecated — adaptive thinking replaces it. Do NOT use budget_tokens and do NOT switch to an older model.
Effort parameter (GA, no beta header): Controls thinking depth and overall token spend via output_config: {effort: "low"|"medium"|"high"|"max"} (inside output_config, not top-level). Default is high (equivalent to omitting it). max is Opus 4.6 only. Works on Opus 4.5, Opus 4.6, and Sonnet 4.6. Will error on Sonnet 4.5 / Haiku 4.5. Combine with adaptive thinking for the best cost-quality tradeoffs. Use low for subagents or simple tasks; max for the deepest reasoning.
Sonnet 4.6: Supports adaptive thinking (thinking: {type: "adaptive"}). budget_tokens is deprecated on Sonnet 4.6 — use adaptive thinking instead.
Older models (only if explicitly requested): If the user specifically asks for Sonnet 4.5 or another older model, use thinking: {type: "enabled", budget_tokens: N}. budget_tokens must be less than max_tokens (minimum 1024). Never choose an older model just because the user mentions budget_tokens — use Opus 4.6 with adaptive thinking instead.
Compaction (Quick Reference)
Beta, Opus 4.6 only. For long-running conversations that may exceed the 200K context window, enable server-side compaction. The API automatically summarizes earlier context when it approaches the trigger threshold (default: 150K tokens). Requires beta header compact-2026-01-12.
Critical: Append response.content (not just the text) back to your messages on every turn. Compaction blocks in the response must be preserved — the API uses them to replace the compacted history on the next request. Extracting only the text string and appending that will silently lose the compaction state.
See {lang}/claude-api/README.md (Compaction section) for code examples. Full docs via WebFetch in shared/live-sources.md.
Reading Guide
After detecting the language, read the relevant files based on what the user needs:
Quick Task Reference
Single text classification/summarization/extraction/Q&A:
→ Read only {lang}/claude-api/README.md
File uploads across multiple requests:
→ Read {lang}/claude-api/README.md + {lang}/claude-api/files-api.md
Agent with built-in tools (file/web/terminal):
→ Read {lang}/agent-sdk/README.md + {lang}/agent-sdk/patterns.md
Claude API (Full File Reference)
Read the language-specific Claude API folder ({language}/claude-api/):
{language}/claude-api/README.md — Read this first. Installation, quick start, common patterns, error handling.
shared/tool-use-concepts.md — Read when the user needs function calling, code execution, memory, or structured outputs. Covers conceptual foundations.
{language}/claude-api/tool-use.md — Read for language-specific tool use code examples (tool runner, manual loop, code execution, memory, structured outputs).
{language}/claude-api/streaming.md — Read when building chat UIs or interfaces that display responses incrementally.
{language}/claude-api/batches.md — Read when processing many requests offline (not latency-sensitive). Runs asynchronously at 50% cost.
{language}/claude-api/files-api.md — Read when sending the same file across multiple requests without re-uploading.
shared/error-codes.md — Read when debugging HTTP errors or implementing error handling.
shared/live-sources.md — WebFetch URLs for fetching the latest official documentation.
Note: For Java, Go, Ruby, C#, PHP, and cURL — these have a single file each covering all basics. Read that file plus shared/tool-use-concepts.md and shared/error-codes.md as needed.
Agent SDK
Read the language-specific Agent SDK folder ({language}/agent-sdk/). Agent SDK is available for Python and TypeScript only.
shared/live-sources.md — WebFetch URLs for current Agent SDK docs.
When to Use WebFetch
Use WebFetch to get the latest documentation when:
User asks for “latest” or “current” information
Cached data seems incorrect
User asks about features not covered here
Live documentation URLs are in shared/live-sources.md.
Common Pitfalls
Don’t truncate inputs when passing files or content to the API. If the content is too long to fit in the context window, notify the user and discuss options (chunking, summarization, etc.) rather than silently truncating.
Opus 4.6 / Sonnet 4.6 thinking: Use thinking: {type: "adaptive"} — do NOT use budget_tokens (deprecated on both Opus 4.6 and Sonnet 4.6). For older models, budget_tokens must be less than max_tokens (minimum 1024). This will throw an error if you get it wrong.
Opus 4.6 prefill removed: Assistant message prefills (last-assistant-turn prefills) return a 400 error on Opus 4.6. Use structured outputs (output_config.format) or system prompt instructions to control response format instead.
128K output tokens: Opus 4.6 supports up to 128K max_tokens, but the SDKs require streaming for large max_tokens to avoid HTTP timeouts. Use .stream() with .get_final_message() / .finalMessage().
Tool call JSON parsing (Opus 4.6): Opus 4.6 may produce different JSON string escaping in tool call input fields (e.g., Unicode or forward-slash escaping). Always parse tool inputs with json.loads() / JSON.parse() — never do raw string matching on the serialized input.
Structured outputs (all models): Use output_config: {format: {...}} instead of the deprecated output_format parameter on messages.create(). This is a general API change, not 4.6-specific.
Don’t reimplement SDK functionality: The SDK provides high-level helpers — use them instead of building from scratch. Specifically: use stream.finalMessage() instead of wrapping .on() events in new Promise(); use typed exception classes (Anthropic.RateLimitError, etc.) instead of string-matching error messages; use SDK types (Anthropic.MessageParam, Anthropic.Tool, Anthropic.Message, etc.) instead of redefining equivalent interfaces.
Don’t define custom types for SDK data structures: The SDK exports types for all API objects. Use Anthropic.MessageParam for messages, Anthropic.Tool for tool definitions, Anthropic.ToolUseBlock / Anthropic.ToolResultBlockParam for tool results, Anthropic.Message for responses. Defining your own interface ChatMessage { role: string; content: unknown } duplicates what the SDK already provides and loses type safety.
Report and document output: For tasks that produce reports, documents, or visualizations, the code execution sandbox has python-docx, python-pptx, matplotlib, pillow, and pypdf pre-installed. Claude can generate formatted files (DOCX, PDF, charts) and return them via the Files API — consider this for “report” or “document” type requests instead of plain stdout text.
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Claude API — C#
Note: The C# SDK is the official Anthropic SDK for C#. Tool use is supported via the Messages API. A class-annotation-based tool runner is not available; use raw tool definitions with JSON schema. The SDK also supports Microsoft.Extensions.AI IChatClient integration with function invocation.
Installation
dotnet add package Anthropic
Client Initialization
using Anthropic;// Default (uses ANTHROPIC_API_KEY env var)AnthropicClient client = new();// Explicit API key (use environment variables — never hardcode keys)AnthropicClient client = new() { ApiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY")};
Basic Message Request
using Anthropic.Models.Messages;var parameters = new MessageCreateParams{ Model = Model.ClaudeOpus4_6, MaxTokens = 1024, Messages = [new() { Role = Role.User, Content = "What is the capital of France?" }]};var message = await client.Messages.Create(parameters);Console.WriteLine(message);
Streaming
using Anthropic.Models.Messages;var parameters = new MessageCreateParams{ Model = Model.ClaudeOpus4_6, MaxTokens = 1024, Messages = [new() { Role = Role.User, Content = "Write a haiku" }]};await foreach (RawMessageStreamEvent streamEvent in client.Messages.CreateStreaming(parameters)){ if (streamEvent.TryPickContentBlockDelta(out var delta) && delta.Delta.TryPickText(out var text)) { Console.Write(text.Text); }}
Tool Use (Manual Loop)
The C# SDK supports raw tool definitions via JSON schema. See the shared tool use concepts for the tool definition format and agentic loop pattern.
Claude API — cURL / Raw HTTP
Use these examples when the user needs raw HTTP requests or is working in a language without an official SDK.
Setup
export ANTHROPIC_API_KEY="your-api-key"
Basic Message Request
curl https://api.anthropic.com/v1/messages \ -H "Content-Type: application/json" \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ -d '{ "model": "claude-opus-4-6", "max_tokens": 1024, "messages": [ {"role": "user", "content": "What is the capital of France?"} ] }'
Opus 4.6 and Sonnet 4.6: Use adaptive thinking. budget_tokens is deprecated on both Opus 4.6 and Sonnet 4.6.
Older models: Use "type": "enabled" with "budget_tokens": N (must be < max_tokens, min 1024).
Beta: The Go SDK provides BetaToolRunner for automatic tool use loops via the toolrunner package.
import ( "context" "fmt" "log" "github.com/anthropics/anthropic-sdk-go" "github.com/anthropics/anthropic-sdk-go/toolrunner")// Define tool input with jsonschema tags for automatic schema generationtype GetWeatherInput struct { City string `json:"city" jsonschema:"required,description=The city name"`}// Create a tool with automatic schema generation from struct tagsweatherTool, err := toolrunner.NewBetaToolFromJSONSchema( "get_weather", "Get current weather for a city", func(ctx context.Context, input GetWeatherInput) (anthropic.BetaToolResultBlockParamContentUnion, error) { return anthropic.BetaToolResultBlockParamContentUnion{ OfText: &anthropic.BetaTextBlockParam{ Text: fmt.Sprintf("The weather in %s is sunny, 72°F", input.City), }, }, nil },)if err != nil { log.Fatal(err)}// Create a tool runner that handles the conversation loop automaticallyrunner := client.Beta.Messages.NewToolRunner( []anthropic.BetaTool{weatherTool}, anthropic.BetaToolRunnerParams{ BetaMessageNewParams: anthropic.BetaMessageNewParams{ Model: anthropic.ModelClaudeOpus4_6, MaxTokens: 1024, Messages: []anthropic.BetaMessageParam{ anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock("What's the weather in Paris?")), }, }, MaxIterations: 5, },)// Run until Claude produces a final responsemessage, err := runner.RunToCompletion(context.Background())if err != nil { log.Fatal(err)}fmt.Println(message.Content[0].Text)
Key features of the Go tool runner:
Automatic schema generation from Go structs via jsonschema tags
RunToCompletion() for simple one-shot usage
All() iterator for processing each message in the conversation
NextMessage() for step-by-step iteration
Streaming variant via NewToolRunnerStreaming() with AllStreaming()
Manual Loop
For fine-grained control, use raw tool definitions via JSON schema. See the shared tool use concepts for the tool definition format and agentic loop pattern.
Claude API — Java
Note: The Java SDK supports the Claude API and beta tool use with annotated classes. Agent SDK is not yet available for Java.
The Java SDK supports beta tool use with annotated classes. Tool classes implement Supplier<String> for automatic execution via BetaToolRunner.
Tool Runner (automatic loop)
import com.anthropic.models.beta.messages.MessageCreateParams;import com.anthropic.models.beta.messages.BetaMessage;import com.anthropic.helpers.BetaToolRunner;import com.fasterxml.jackson.annotation.JsonClassDescription;import com.fasterxml.jackson.annotation.JsonPropertyDescription;import java.util.function.Supplier;@JsonClassDescription("Get the weather in a given location")static class GetWeather implements Supplier<String> { @JsonPropertyDescription("The city and state, e.g. San Francisco, CA") public String location; @Override public String get() { return "The weather in " + location + " is sunny and 72°F"; }}BetaToolRunner toolRunner = client.beta().messages().toolRunner( MessageCreateParams.builder() .model("claude-opus-4-6") .maxTokens(1024L) .putAdditionalHeader("anthropic-beta", "structured-outputs-2025-11-13") .addTool(GetWeather.class) .addUserMessage("What's the weather in San Francisco?") .build());for (BetaMessage message : toolRunner) { System.out.println(message);}
Non-Beta Tool Use
Tool use is also available through the non-beta com.anthropic.models.messages.MessageCreateParams with addTool(Tool) for manually defined JSON schemas, without needing the beta namespace. The beta namespace is only needed for the class-annotation convenience layer (@JsonClassDescription, BetaToolRunner).
Manual Loop
For manual tool loops, define tools as JSON schema in the request, handle tool_use blocks in the response, send tool_result back, and loop until stop_reason is "end_turn". See the shared tool use concepts for the agentic loop pattern.
Claude API — PHP
Note: The PHP SDK is the official Anthropic SDK for PHP. Tool runner and Agent SDK are not available. Bedrock, Vertex AI, and Foundry clients are supported.
Installation
composer require "anthropic-ai/sdk"
Client Initialization
use Anthropic\Client;// Using API key from environment variable$client = new Client(apiKey: getenv("ANTHROPIC_API_KEY"));
Amazon Bedrock
use Anthropic\BedrockClient;$client = new BedrockClient( region: 'us-east-1',);
Google Vertex AI
use Anthropic\VertexClient;$client = new VertexClient( region: 'us-east5', projectId: 'my-project-id',);
Anthropic Foundry
use Anthropic\FoundryClient;$client = new FoundryClient( authToken: getenv("ANTHROPIC_AUTH_TOKEN"),);
Basic Message Request
$message = $client->messages->create( model: 'claude-opus-4-6', maxTokens: 1024, messages: [ ['role' => 'user', 'content' => 'What is the capital of France?'], ],);echo $message->content[0]->text;
The PHP SDK supports raw tool definitions via JSON schema. See the shared tool use concepts for the tool definition format and agentic loop pattern.
Claude API — Ruby
Note: The Ruby SDK supports the Claude API. A tool runner is available in beta via client.beta.messages.tool_runner(). Agent SDK is not yet available for Ruby.
The Ruby SDK supports tool use via raw JSON schema definitions and also provides a beta tool runner for automatic tool execution.
Tool Runner (Beta)
class GetWeatherInput < Anthropic::BaseModel required :location, String, doc: "City and state, e.g. San Francisco, CA"endclass GetWeather < Anthropic::BaseTool doc "Get the current weather for a location" input_schema GetWeatherInput def call(input) "The weather in #{input.location} is sunny and 72°F." endendclient.beta.messages.tool_runner( model: :"claude-opus-4-6", max_tokens: 1024, tools: [GetWeather.new], messages: [{ role: "user", content: "What's the weather in San Francisco?" }]).each_message do |message| puts message.contentend
This file documents HTTP error codes returned by the Claude API, their common causes, and how to handle them. For language-specific error handling examples, see the python/ or typescript/ folders.
Invalid parameter types (e.g., string where integer expected)
Empty messages array
Messages not alternating user/assistant
Example error:
{ "type": "error", "error": { "type": "invalid_request_error", "message": "messages: roles must alternate between \"user\" and \"assistant\"" }}
Fix: Validate request structure before sending. Check that:
model is a valid model ID
max_tokens is a positive integer
messages array is non-empty and alternates correctly
401 Unauthorized
Causes:
Missing x-api-key header or Authorization header
Invalid API key format
Revoked or deleted API key
Fix: Ensure ANTHROPIC_API_KEY environment variable is set correctly.
403 Forbidden
Causes:
API key doesn't have access to the requested model
Organization-level restrictions
Attempting to access beta features without beta access
Fix: Check your API key permissions in the Console. You may need a different API key or to request access to specific features.
404 Not Found
Causes:
Typo in model ID (e.g., claude-sonnet-4.6 instead of claude-sonnet-4-6)
Using deprecated model ID
Invalid API endpoint
Fix: Use exact model IDs from the models documentation. You can use aliases (e.g., claude-opus-4-6).
413 Request Too Large
Causes:
Request body exceeds maximum size
Too many tokens in input
Image data too large
Fix: Reduce input size — truncate conversation history, compress/resize images, or split large documents into chunks.
400 Validation Errors
Some 400 errors are specifically related to parameter validation:
max_tokens exceeds model's limit
Invalid temperature value (must be 0.0-1.0)
budget_tokens >= max_tokens in extended thinking
Invalid tool definition schema
Common mistake with extended thinking:
# Wrong: budget_tokens must be < max_tokensthinking: budget_tokens=10000, max_tokens=1000 → Error!# Correctthinking: budget_tokens=10000, max_tokens=16000
429 Rate Limited
Causes:
Exceeded requests per minute (RPM)
Exceeded tokens per minute (TPM)
Exceeded tokens per day (TPD)
Headers to check:
retry-after: Seconds to wait before retrying
x-ratelimit-limit-*: Your limits
x-ratelimit-remaining-*: Remaining quota
Fix: The Anthropic SDKs automatically retry 429 and 5xx errors with exponential backoff (default: max_retries=2). For custom retry behavior, see the language-specific error handling examples.
Fix: Retry with exponential backoff. Consider using a different model (Haiku is often less loaded), spreading requests over time, or implementing request queuing.
Common Mistakes and Fixes
Mistake
Error
Fix
budget_tokens >= max_tokens
400
Ensure budget_tokens < max_tokens
Typo in model ID
404
Use valid model ID like claude-opus-4-6
First message is assistant
400
First message must be user
Consecutive same-role messages
400
Alternate user and assistant
API key in code
401 (leaked key)
Use environment variable
Custom retry needs
429/5xx
SDK retries automatically; customize with max_retries
Typed Exceptions in SDKs
Always use the SDK's typed exception classes instead of checking error messages with string matching. Each HTTP error code maps to a specific exception class:
All exception classes extend Anthropic.APIError, which has a status property. Use instanceof checks from most specific to least specific (e.g., check RateLimitError before APIError).
Live Documentation Sources
This file contains WebFetch URLs for fetching current information from platform.claude.com and Agent SDK repositories. Use these when users need the latest data that may have changed since the cached content was last updated.
When to Use WebFetch
User explicitly asks for "latest" or "current" information
Cached data seems incorrect
User asks about features not covered in cached content
Use cached content from the language-specific files (note the cache date)
Inform user the data may be outdated
Suggest they check platform.claude.com or the GitHub repos directly
Claude Model Catalog
Only use exact model IDs listed in this file. Never guess or construct model IDs — incorrect IDs will cause API errors. Use aliases wherever available. For the latest information, WebFetch the Models Overview URL in shared/live-sources.md.
Current Models (recommended)
Friendly Name
Alias (use this)
Full ID
Context
Max Output
Status
Claude Opus 4.6
claude-opus-4-6
—
200K (1M beta)
128K
Active
Claude Sonnet 4.6
claude-sonnet-4-6
-
200K (1M beta)
64K
Active
Claude Haiku 4.5
claude-haiku-4-5
claude-haiku-4-5-20251001
200K
64K
Active
Model Descriptions
Claude Opus 4.6 — Our most intelligent model for building agents and coding. Supports adaptive thinking (recommended), 128K max output tokens (requires streaming for large outputs). 1M context window available in beta via context-1m-2025-08-07 header.
Claude Sonnet 4.6 — Our best combination of speed and intelligence. Supports adaptive thinking (recommended). 1M context window available in beta via context-1m-2025-08-07 header. 64K max output tokens.
Claude Haiku 4.5 — Fastest and most cost-effective model for simple tasks.
Legacy Models (still active)
Friendly Name
Alias (use this)
Full ID
Status
Claude Opus 4.5
claude-opus-4-5
claude-opus-4-5-20251101
Active
Claude Opus 4.1
claude-opus-4-1
claude-opus-4-1-20250805
Active
Claude Sonnet 4.5
claude-sonnet-4-5
claude-sonnet-4-5-20250929
Active
Claude Sonnet 4
claude-sonnet-4-0
claude-sonnet-4-20250514
Active
Claude Opus 4
claude-opus-4-0
claude-opus-4-20250514
Active
Deprecated Models (retiring soon)
Friendly Name
Alias (use this)
Full ID
Status
Claude Haiku 3
—
claude-3-haiku-20240307
Deprecated
Retired Models (no longer available)
Friendly Name
Full ID
Retired
Claude Sonnet 3.7
claude-3-7-sonnet-20250219
Feb 19, 2026
Claude Haiku 3.5
claude-3-5-haiku-20241022
Feb 19, 2026
Claude Opus 3
claude-3-opus-20240229
Jan 5, 2026
Claude Sonnet 3.5
claude-3-5-sonnet-20241022
Oct 28, 2025
Claude Sonnet 3.5
claude-3-5-sonnet-20240620
Oct 28, 2025
Claude Sonnet 3
claude-3-sonnet-20240229
Jul 21, 2025
Claude 2.1
claude-2.1
Jul 21, 2025
Claude 2.0
claude-2.0
Jul 21, 2025
Resolving User Requests
When a user asks for a model by name, use this table to find the correct model ID:
User says...
Use this model ID
"opus", "most powerful"
claude-opus-4-6
"opus 4.6"
claude-opus-4-6
"opus 4.5"
claude-opus-4-5
"opus 4.1"
claude-opus-4-1
"opus 4", "opus 4.0"
claude-opus-4-0
"sonnet", "balanced"
claude-sonnet-4-6
"sonnet 4.6"
claude-sonnet-4-6
"sonnet 4.5"
claude-sonnet-4-5
"sonnet 4", "sonnet 4.0"
claude-sonnet-4-0
"sonnet 3.7"
Retired — suggest claude-sonnet-4-5
"sonnet 3.5"
Retired — suggest claude-sonnet-4-5
"haiku", "fast", "cheap"
claude-haiku-4-5
"haiku 4.5"
claude-haiku-4-5
"haiku 3.5"
Retired — suggest claude-haiku-4-5
"haiku 3"
Deprecated — suggest claude-haiku-4-5
Tool Use Concepts
This file covers the conceptual foundations of tool use with the Claude API. For language-specific code examples, see the python/, typescript/, or other language folders.
User-Defined Tools
Tool Definition Structure
Note: When using the Tool Runner (beta), tool schemas are generated automatically from your function signatures (Python), Zod schemas (TypeScript), annotated classes (Java), jsonschema struct tags (Go), or BaseTool subclasses (Ruby). The raw JSON schema format below is for the manual approach or SDKs without tool runner support.
Each tool requires a name, description, and JSON Schema for its inputs:
{ "name": "get_weather", "description": "Get current weather for a location", "input_schema": { "type": "object", "properties": { "location": { "type": "string", "description": "City and state, e.g., San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit" } }, "required": ["location"] }}
Best practices for tool definitions:
Use clear, descriptive names (e.g., get_weather, search_database, send_email)
Write detailed descriptions — Claude uses these to decide when to use the tool
Include descriptions for each property
Use enum for parameters with a fixed set of values
Mark truly required parameters in required; make others optional with defaults
Tool Choice Options
Control when Claude uses tools:
Value
Behavior
{"type": "auto"}
Claude decides whether to use tools (default)
{"type": "any"}
Claude must use at least one tool
{"type": "tool", "name": "..."}
Claude must use the specified tool
{"type": "none"}
Claude cannot use tools
Any tool_choice value can also include "disable_parallel_tool_use": true to force Claude to use at most one tool per response. By default, Claude may request multiple tool calls in a single response.
Tool Runner vs Manual Loop
Tool Runner (Recommended): The SDK's tool runner handles the agentic loop automatically — it calls the API, detects tool use requests, executes your tool functions, feeds results back to Claude, and repeats until Claude stops calling tools. Available in Python, TypeScript, Java, Go, and Ruby SDKs (beta). The Python SDK also provides MCP conversion helpers (anthropic.lib.tools.mcp) to convert MCP tools, prompts, and resources for use with the tool runner — see python/claude-api/tool-use.md for details.
Manual Agentic Loop: Use when you need fine-grained control over the loop (e.g., custom logging, conditional tool execution, human-in-the-loop approval). Loop until stop_reason == "end_turn", always append the full response.content to preserve tool_use blocks, and ensure each tool_result includes the matching tool_use_id.
Stop reasons for server-side tools: When using server-side tools (code execution, web search, etc.), the API runs a server-side sampling loop. If this loop reaches its default limit of 10 iterations, the response will have stop_reason: "pause_turn". To continue, re-send the user message and assistant response and make another API request — the server will resume where it left off. Do NOT add an extra user message like "Continue." — the API detects the trailing server_tool_use block and knows to resume automatically.
# Handle pause_turn in your agentic loopif response.stop_reason == "pause_turn": messages = [ {"role": "user", "content": user_query}, {"role": "assistant", "content": response.content}, ] # Make another API request — server resumes automatically response = client.messages.create( model="claude-opus-4-6", messages=messages, tools=tools )
Set a max_continuations limit (e.g., 5) to prevent infinite loops. For the full guide, see: https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons
Security: The tool runner executes your tool functions automatically whenever Claude requests them. For tools with side effects (sending emails, modifying databases, financial transactions), validate inputs within your tool functions and consider requiring confirmation for destructive operations. Use the manual agentic loop if you need human-in-the-loop approval before each tool execution.
Handling Tool Results
When Claude uses a tool, the response contains a tool_use block. You must:
Execute the tool with the provided input
Send the result back in a tool_result message
Continue the conversation
Error handling in tool results: When a tool execution fails, set "is_error": true and provide an informative error message. Claude will typically acknowledge the error and either try a different approach or ask for clarification.
Multiple tool calls: Claude can request multiple tools in a single response. Handle them all before continuing — send all results back in a single user message.
Server-Side Tools: Code Execution
The code execution tool lets Claude run code in a secure, sandboxed container. Unlike user-defined tools, server-side tools run on Anthropic's infrastructure — you don't execute anything client-side. Just include the tool definition and Claude handles the rest.
Key Facts
Runs in an isolated container (1 CPU, 5 GiB RAM, 5 GiB disk)
No internet access (fully sandboxed)
Python 3.11 with data science libraries pre-installed
Containers persist for 30 days and can be reused across requests
Free when used with web search/web fetch tools; otherwise $0.05/hour after 1,550 free hours/month per organization
Tool Definition
The tool requires no schema — just declare it in the tools array:
Additional packages can be installed at runtime via pip install.
Supported File Types for Upload
Type
Extensions
Data
CSV, Excel (.xlsx/.xls), JSON, XML
Images
JPEG, PNG, GIF, WebP
Text
.txt, .md, .py, .js, etc.
Container Reuse
Reuse containers across requests to maintain state (files, installed packages, variables). Extract the container_id from the first response and pass it to subsequent requests.
Response Structure
The response contains interleaved text and tool result blocks:
text — Claude's explanation
server_tool_use — What Claude is doing
bash_code_execution_tool_result — Code execution output (check return_code for success/failure)
Security: Always sanitize filenames with os.path.basename() / path.basename() before writing downloaded files to disk to prevent path traversal attacks. Write files to a dedicated output directory.
Server-Side Tools: Web Search and Web Fetch
Web search and web fetch let Claude search the web and retrieve page content. They run server-side — just include the tool definitions and Claude handles queries, fetching, and result processing automatically.
The web_search_20260209 and web_fetch_20260209 versions support dynamic filtering — Claude writes and executes code to filter search results before they reach the context window, improving accuracy and token efficiency. Dynamic filtering is built into these tool versions and activates automatically; you do not need to separately declare the code_execution tool or pass any beta header.
Without dynamic filtering, the previous web_search_20250305 version is also available.
Note: Only include the standalone code_execution tool when your application needs code execution for its own purposes (data analysis, file processing, visualization) independent of web search. Including it alongside _20260209 web tools creates a second execution environment that can confuse the model.
Server-Side Tools: Programmatic Tool Calling
Programmatic tool calling lets Claude execute complex multi-tool workflows in code, keeping intermediate results out of the context window. Claude writes code that calls your tools directly, reducing token usage for multi-step operations.
The tool search tool lets Claude dynamically discover tools from large libraries without loading all definitions into the context window. Useful when you have many tools but only a few are relevant to any given query.
You can provide sample tool calls directly in your tool definitions to demonstrate usage patterns and reduce parameter errors. This helps Claude understand how to correctly format tool inputs, especially for tools with complex schemas.
Computer use lets Claude interact with a desktop environment (screenshots, mouse, keyboard). It can be Anthropic-hosted (server-side, like code execution) or self-hosted (you provide the environment and execute actions client-side).
The memory tool enables Claude to store and retrieve information across conversations through a memory file directory. Claude can create, read, update, and delete files that persist between sessions.
Key Facts
Client-side tool — you control storage via your implementation
The SDKs provide helper classes/functions for implementing the memory backend
Security: Never store API keys, passwords, tokens, or other secrets in memory files. Be cautious with personally identifiable information (PII) — check data privacy regulations (GDPR, CCPA) before persisting user data. The reference implementations have no built-in access control; in multi-user systems, implement per-user memory directories and authentication in your tool handlers.
Structured outputs constrain Claude's responses to follow a specific JSON schema, guaranteeing valid, parseable output. This is not a separate tool — it enhances the Messages API response format and/or tool parameter validation.
Two features are available:
JSON outputs (output_config.format): Control Claude's response format
Strict tool use (strict: true): Guarantee valid tool parameter schemas
Supported models: Claude Opus 4.6, Claude Sonnet 4.6, and Claude Haiku 4.5. Legacy models (Claude Opus 4.5, Claude Opus 4.1) also support structured outputs.
Recommended: Use client.messages.parse() which automatically validates responses against your schema. When using messages.create() directly, use output_config: {format: {...}}. The output_format convenience parameter is also accepted by some SDK methods (e.g., .parse()), but output_config.format is the canonical API-level parameter.
additionalProperties set to anything other than false
The Python and TypeScript SDKs automatically handle unsupported constraints by removing them from the schema sent to the API and validating them client-side.
Important Notes
First request latency: New schemas incur a one-time compilation cost. Subsequent requests with the same schema use a 24-hour cache.
Refusals: If Claude refuses for safety reasons (stop_reason: "refusal"), the output may not match your schema.
Token limits: If stop_reason: "max_tokens", output may be incomplete. Increase max_tokens.