> For the complete documentation index, see [llms.txt](/llms.txt).
> A full single-fetch corpus is available at [llms-full.txt](/llms-full.txt).
---
last_verified: 2026-10-09
title: LM
description: Call language models directly with agnt5.lm — generate, stream, structured output, reasoning effort, and prompts
category: Python SDK
hideRightSidebar: true
---

`agnt5.lm` calls a language model directly. Calls made inside a handler are traced in the run. Models are named `provider/model`, and each provider reads its API key from the environment, for example `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, or `GOOGLE_API_KEY`.

## `lm.generate(...)`

```python
from agnt5 import FunctionContext, function, lm


@function
async def pick_color(ctx: FunctionContext) -> str:
    response = await lm.generate(
        model="openai/gpt-6-luna",
        system_prompt="Reply in five words or fewer.",
        prompt="Name a primary color.",
    )
    return response.text
```

| Parameter | Description |
|---|---|
| `model` | The model, as `provider/model`. |
| `prompt` | The user message as a string, or a stored [`Prompt`](/docs/build/prompts.md). |
| `system_prompt` | System instructions. |
| `messages` | A conversation instead of `prompt`: `[{"role": "user", "content": "..."}]`. |
| `response_format` | Ask for JSON output. See below. |
| `reasoning_effort` | `ReasoningEffort.LOW` and the other levels, for models that reason. |
| `cache` | [Prompt caching](/docs/build/prompt-caching.md) policy. |

| Response field | Description |
|---|---|
| `text` | The model's reply. |
| `usage` | `prompt_tokens`, `completion_tokens`, `total_tokens`, `cached_tokens`, and `cache_creation_tokens`. |
| `finish_reason` | Why generation stopped. |
| `tool_calls` | Tool calls the model made. |
| `structured_output` | The parsed object when you asked for JSON output. |

### Structured output

```python
response = await lm.generate(
    model="openai/gpt-6-luna",
    prompt="Give the capital of France.",
    response_format={
        "type": "json_schema",
        "name": "capital",
        "schema": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
            "additionalProperties": False,
        },
    },
)
# response.structured_output == {"city": "Paris"}
```

### Reasoning effort

```python
from agnt5.lm import ReasoningEffort

response = await lm.generate(
    model="openai/gpt-6-luna",
    prompt="What is 17 * 23?",
    reasoning_effort=ReasoningEffort.LOW,
)
```

### Stored prompts

```python
from agnt5.lm import Prompt

response = await lm.generate(
    model="openai/gpt-6-luna",
    prompt=Prompt(id="support_reply", variables={"topic": "shipping"}),
)
```

See [Prompts](/docs/build/prompts.md) for versions and overrides.

## `lm.stream(...)`

An async generator of events. `lm.content_block.delta` events carry the text as it arrives, and `lm.completed` ends the stream:

```python
text = ""
async for event in lm.stream(model="openai/gpt-6-luna", prompt="Count from 1 to 5."):
    if event.event_type == "lm.content_block.delta":
        text += event.content
```

## Next steps

* [Agents](/sdk/python/agents.md): run a model in a loop with tools.
* [Prompts](/docs/build/prompts.md): store, version, and override prompts.
* [Prompt caching](/docs/build/prompt-caching.md): reuse a stable prefix across calls.
