← Back to Hub Tool Reference
🔌

Claude API

Integrate Claude directly into your applications, scripts, and services using the Anthropic API — with streaming, tool use, vision, and prompt caching.

REST API Streaming Tool Use Vision Caching

🧠 What It Is

The Claude API (formerly Anthropic API) gives you direct programmatic access to Claude models. You send messages, Claude responds — with full control over model selection, system prompts, temperature, streaming, tool definitions, and vision inputs. It's the foundation for every Claude-powered product.

The official Python and TypeScript SDKs make integration simple: pip install anthropic and you're one function call away from a production-grade AI response. All models support streaming, multi-turn conversations, tool use, image inputs, and prompt caching for 90% cost reduction on repeated context.

Latest models (2025): Always default to the most capable Claude 4.X models — Opus 4.7, Sonnet 4.6, and Haiku 4.5 — for best results. Sonnet 4.6 is the ideal balance of capability and speed for most production use cases.

🤖 Current Model Family

Claude Opus 4.7
claude-opus-4-7
Most powerful. Complex reasoning, nuanced writing, agentic tasks.
Most capable
Claude Sonnet 4.6
claude-sonnet-4-6
Best balance of intelligence and speed for production apps.
Recommended
Claude Haiku 4.5
claude-haiku-4-5-20251001
Fastest and most cost-efficient for high-volume, simpler tasks.
Fastest

🛠️ 5 Real-World Examples

Example 01

Basic API Call (Python)

The simplest possible integration — send a message, get a response. The starting point for any Claude-powered feature.

python
import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain JWT authentication"}
    ]
)

print(message.content[0].text)
Example 02

Streaming Response

Stream Claude's output token-by-token for a responsive chat feel — essential for user-facing apps where latency matters.

python
with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": prompt}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
Example 03

Tool Use (Function Calling)

Define tools and Claude decides when to call them — enabling AI-driven workflows that interact with databases, APIs, and external systems.

python
tools = [{
    "name": "get_weather",
    "description": "Get current weather for a city",
    "input_schema": {
        "type": "object",
        "properties": {
            "city": {"type": "string"}
        },
        "required": ["city"]
    }
}]

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user",
               "content": "What's the weather in Paris?"}]
)
Example 04

Vision — Analyze an Image

Pass images directly to Claude via base64 or URL. Ideal for OCR, UI feedback analysis, chart interpretation, and multimodal apps.

python
message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{
        "role": "user",
        "content": [
            {
                "type": "image",
                "source": {
                    "type": "base64",
                    "media_type": "image/png",
                    "data": image_data
                }
            },
            {"type": "text",
             "text": "Describe this UI and identify issues"}
        ]
    }]
)
Example 05

Prompt Caching — 90% Cost Reduction

Mark large, repeated context (system prompts, documents, examples) as cacheable. Pay full price once, then 10% on every subsequent call that reuses it.

python
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system=[{
        "type": "text",
        "text": large_system_prompt,  # 10K+ tokens
        "cache_control": {"type": "ephemeral"}
    }],
    messages=[{
        "role": "user",
        "content": user_question
    }]
)
# cache_read_input_tokens → billed at 10% of normal
🔌 API Call Simulator interactive
Claude API Sandbox
Simulate API calls and see response metadata.
$ _
$
Best practice: Always pin a specific model ID in production (never use "claude-latest"). Set max_tokens explicitly to avoid unexpected costs. Use ANTHROPIC_API_KEY env var — never hardcode your key.