> For the complete documentation index, see [llms.txt](https://docs.mozilla.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mozilla.ai/api-reference/messages.md).

# Messages

Anthropic Messages API for all providers

The `messages` and `amessages` functions use the Anthropic Messages API format. All providers support this through automatic conversion, so you can use the same Anthropic-style message format regardless of backend.

### `any_llm.messages()`

```
def messages(
    model: str,
    messages: list[dict[str, Any]],
    max_tokens: int,
    *,
    provider: str | LLMProvider | None = None,
    system: str | list[dict[str, Any]] | None = None,
    temperature: float | None = None,
    top_p: float | None = None,
    top_k: int | None = None,
    stream: bool | None = None,
    stop_sequences: list[str] | None = None,
    tools: list[dict[str, Any]] | None = None,
    tool_choice: dict[str, Any] | None = None,
    metadata: dict[str, Any] | None = None,
    thinking: dict[str, Any] | None = None,
    cache_control: dict[str, Any] | None = None,
    prompt_cache_key: str | None = None,
    service_tier: str | None = None,
    context_management: dict[str, Any] | None = None,
    betas: list[str] | None = None,
    container: str | dict[str, Any] | None = None,
    inference_geo: str | None = None,
    output_format: type | dict[str, Any] | None = None,
    timeout: float | None = None,
    api_key: str | None = None,
    api_base: str | None = None,
    client_args: dict[str, Any] | None = None,
    **kwargs: Any,
) -> MessageResponse | ParsedMessage[Any] | ParsedBetaMessage[Any] | Iterator[MessageStartEvent | MessageDeltaEvent | MessageStopEvent | ContentBlockStartEvent | ContentBlockDeltaEvent | ContentBlockStopEvent]
```

### `any_llm.amessages()`

Async variant with the same parameters. Returns `MessageResponse | AsyncIterator[MessageStreamEvent]`.

```
async def amessages(
    model: str,
    messages: list[dict[str, Any]],
    max_tokens: int,
    *,
    provider: str | LLMProvider | None = None,
    system: str | list[dict[str, Any]] | None = None,
    temperature: float | None = None,
    top_p: float | None = None,
    top_k: int | None = None,
    stream: bool | None = None,
    stop_sequences: list[str] | None = None,
    tools: list[dict[str, Any]] | None = None,
    tool_choice: dict[str, Any] | None = None,
    metadata: dict[str, Any] | None = None,
    thinking: dict[str, Any] | None = None,
    cache_control: dict[str, Any] | None = None,
    prompt_cache_key: str | None = None,
    service_tier: str | None = None,
    context_management: dict[str, Any] | None = None,
    betas: list[str] | None = None,
    container: str | dict[str, Any] | None = None,
    inference_geo: str | None = None,
    output_format: type | dict[str, Any] | None = None,
    timeout: float | None = None,
    api_key: str | None = None,
    api_base: str | None = None,
    client_args: dict[str, Any] | None = None,
    **kwargs: Any,
) -> MessageResponse | ParsedMessage[Any] | ParsedBetaMessage[Any] | AsyncIterator[MessageStartEvent | MessageDeltaEvent | MessageStopEvent | ContentBlockStartEvent | ContentBlockDeltaEvent | ContentBlockStopEvent]
```

### Parameters

| Parameter            | Type                                  | Default    | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| -------------------- | ------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `model`              | `str`                                 | *required* | Model identifier. **Recommended**: Use with separate `provider` parameter. **Alternative**: Combined format 'provider:model'.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `messages`           | `list[dict[str, Any]]`                | *required* | List of messages in Anthropic format.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `max_tokens`         | `int`                                 | *required* | Maximum number of tokens to generate.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `provider`           | `str \| LLMProvider \| None`          | None       | Provider name to use for the request.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `system`             | `str \| list[dict[str, Any]] \| None` | None       | System prompt (string or list of content blocks with optional cache\_control).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `temperature`        | `float \| None`                       | None       | Controls randomness. Anthropic deprecates this for current Claude models.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `top_p`              | `float \| None`                       | None       | Controls nucleus sampling. Anthropic deprecates this for current Claude models.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `top_k`              | `int \| None`                         | None       | Restricts sampling to the top K options. Anthropic deprecates this for current Claude models.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `stream`             | `bool \| None`                        | None       | Whether to stream the response.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `stop_sequences`     | `list[str] \| None`                   | None       | Custom stop sequences.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `tools`              | `list[dict[str, Any]] \| None`        | None       | List of tools in Anthropic format.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| `tool_choice`        | `dict[str, Any] \| None`              | None       | Controls which tool the model uses.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `metadata`           | `dict[str, Any] \| None`              | None       | Request metadata.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `thinking`           | `dict[str, Any] \| None`              | None       | Thinking/reasoning configuration.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `cache_control`      | `dict[str, Any] \| None`              | None       | Cache control configuration for prompt caching.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `prompt_cache_key`   | `str \| None`                         | None       | A key to use when reading from or writing to a provider's prompt cache.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| `service_tier`       | `str \| None`                         | None       | The service tier to use for this request.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `context_management` | `dict[str, Any] \| None`              | None       | Anthropic context management configuration. The `compact_20260112` strategy requires a supported model. Its `input_tokens` trigger value must be at least 50,000 when provided; see [Anthropic's compaction documentation](https://platform.claude.com/docs/en/build-with-claude/compaction).                                                                                                                                                                                                                                                                                                                                                    |
| `betas`              | `list[str] \| None`                   | None       | Anthropic beta identifiers.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `container`          | `str \| dict[str, Any] \| None`       | None       | Container identifier, or an object with optional `id` and `skills`. A string reuses an existing container. An object selects Skills for a fresh container or reuses one while attaching Skills. See [Anthropic's Skills container parameter](https://platform.claude.com/docs/en/build-with-claude/skills-guide#container-parameter).                                                                                                                                                                                                                                                                                                            |
| `inference_geo`      | `str \| None`                         | None       | Anthropic's region to run the request in, such as `us`. When omitted, Anthropic uses the workspace's `default_inference_geo`. The response reports where the request ran in `usage.inference_geo`. Providers without a matching option raise `UnsupportedParameterError`.                                                                                                                                                                                                                                                                                                                                                                        |
| `output_format`      | `type \| dict[str, Any] \| None`      | None       | Structured output, mirroring Anthropic's `messages.parse`/`output_config`. Either a Pydantic `BaseModel`/dataclass **type** (typed `parsed_output`) or a raw Anthropic `output_config` **dict** for non-Pydantic JSON schemas (`parsed_output` holds the parsed JSON). Non-streaming calls return Anthropic's `ParsedMessage` for types or mappings with a non-empty schema dict. Mappings without one return `MessageResponse`. Providers with native support can stream schema-constrained Messages events. Native Anthropic typed beta requests return `ParsedBetaMessage` when `context_management` is set or beta identifiers are supplied. |
| `timeout`            | `float \| None`                       | None       | Per-request timeout in seconds, passed through to the provider's client/SDK. An explicit `None` is treated the same as omitting it (the provider's default applies), so it cannot request an unbounded timeout. Providers that have no per-request timeout raise `UnsupportedParameterError`; set a timeout on their client via `client_args` instead.                                                                                                                                                                                                                                                                                           |
| `api_key`            | `str \| None`                         | None       | API key for the provider.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `api_base`           | `str \| None`                         | None       | Base URL for the provider API.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `client_args`        | `dict[str, Any] \| None`              | None       | Additional provider-specific arguments for client instantiation.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `**kwargs`           | `Any`                                 | *required* | Additional provider-specific arguments.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |

### Return Value

* **Non-streaming**: Returns a [`MessageResponse`](https://docs.mozilla.ai/api-reference/completion-1/messages.md) object.
* **Streaming** (`stream=True`): Returns an `Iterator[MessageStreamEvent]` (sync) or `AsyncIterator[MessageStreamEvent]` (async).

### Usage

#### Basic message

```python
from any_llm.api import messages

response = messages(
    model="claude-sonnet-4-20250514",
    provider="anthropic",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=1024,
)
print(response.content[0].text)
```

#### With system prompt

```python
response = messages(
    model="claude-sonnet-4-20250514",
    provider="anthropic",
    messages=[{"role": "user", "content": "Translate to French: Hello"}],
    max_tokens=1024,
    system="You are a professional translator.",
)
```

#### Async

```python
import asyncio
from any_llm.api import amessages

async def main():
    response = await amessages(
        model="claude-sonnet-4-20250514",
        provider="anthropic",
        messages=[{"role": "user", "content": "Hello!"}],
        max_tokens=1024,
    )
    print(response.content[0].text)

asyncio.run(main())
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mozilla.ai/api-reference/messages.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
