> ## Documentation Index
> Fetch the complete documentation index at: https://thethirdpenco-feat-tool-lifecycle-events.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic

> Use Claude models with ai-query

The Anthropic provider gives you access to Claude models, known for their strong reasoning capabilities and safety features.

## Reasoning

Claude reasoning support in ai-query is intentionally provider-level, not model-version-gated.

Examples:

```python theme={null}
result = await generate_text(
    model=anthropic("claude-opus-4-7"),
    prompt="Analyze this carefully.",
    reasoning={"effort": "medium"},
    max_tokens=4096,
)
```

```python theme={null}
result = await generate_text(
    model=anthropic("claude-3-7-sonnet-20250219"),
    prompt="Think through this.",
    reasoning={"budget": 8000},
    max_tokens=16000,
)
```

Notes:

* normalized `effort` maps to Anthropic's `output_config.effort`
* normalized `budget` maps to `thinking.budget_tokens`
* if a specific Claude model rejects a combination, Anthropic returns the validation error directly
* thinking visibility controls like summarized vs omitted remain provider-specific and should still be configured through `provider_options["anthropic"]`

For ai-query's streaming thinking output model, see [`Thinking`](/core/thinking).

## Setup

Set your API key as an environment variable:

```bash theme={null}
export ANTHROPIC_API_KEY="sk-ant-..."
```

## Usage

```python theme={null}
from ai_query import generate_text
from ai_query.providers import anthropic

result = await generate_text(
    model=anthropic("claude-4-5-sonnet-20250929"),
    prompt="Explain the theory of relativity."
)
```

## Available Models

| Model                        | Description                                     |
| ---------------------------- | ----------------------------------------------- |
| `claude-4-5-opus-20251101`   | Claude 4.5 Opus - most capable                  |
| `claude-4-5-sonnet-20250929` | Claude 4.5 Sonnet - powerful with Thinking mode |
| `claude-4-5-haiku-20251001`  | Claude 4.5 Haiku - fast and efficient           |
| `claude-4-opus-20250514`     | Claude 4 Opus - high capability                 |
| `claude-4-sonnet-20250514`   | Claude 4 Sonnet - balanced performance          |
| `claude-3-7-sonnet-20250219` | Claude 3.7 Sonnet - stable                      |
| `claude-3-5-sonnet-latest`   | Claude 3.5 Sonnet - reliable                    |
| `claude-3-5-haiku-latest`    | Claude 3.5 Haiku - fastest                      |

## Provider Options

Customize Anthropic-specific parameters:

```python theme={null}
result = await generate_text(
    model=anthropic("claude-sonnet-4-20250514"),
    prompt="Write a detailed analysis.",
    provider_options={
        "anthropic": {
            "temperature": 0.7,
            "max_tokens": 2000,
            "top_p": 0.9
        }
    }
)
```

### Supported Options

| Option        | Type    | Default | Description           |
| ------------- | ------- | ------- | --------------------- |
| `temperature` | `float` | `1.0`   | Randomness (0-1)      |
| `max_tokens`  | `int`   | `4096`  | Maximum output tokens |
| `top_p`       | `float` | `1.0`   | Nucleus sampling      |

## Tool Calling

Claude models support powerful tool calling:

```python theme={null}
from ai_query import generate_text
from ai_query.providers import anthropic, tool, Field

@tool(description="Calculate a math expression")
async def calculate(
    expression: str = Field(description="Math expression to evaluate")
) -> str:
    result = eval(expression)  # Use a safe evaluator in production
    return str(result)

result = await generate_text(
    model=anthropic("claude-sonnet-4-20250514"),
    prompt="What is (15 * 8) + (42 / 6)?",
    tools={"calculate": calculate}
)
```

## Streaming

```python theme={null}
from ai_query import stream_text
from ai_query.providers import anthropic

result = stream_text(
    model=anthropic("claude-sonnet-4-20250514"),
    prompt="Explain quantum entanglement step by step."
)

async for chunk in result.text_stream:
    print(chunk, end="", flush=True)
```

## System Prompts

Claude responds well to detailed system prompts:

```python theme={null}
result = await generate_text(
    model=anthropic("claude-sonnet-4-20250514"),
    system="""You are an expert Python developer.
    Always provide working code examples.
    Explain your reasoning step by step.""",
    prompt="How do I implement a binary search tree?"
)
```
