> 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 LM — providers, generate, stream, structured output, and prompts
category: TypeScript SDK
hideRightSidebar: true
---

`LM` is the model client. Agents use it for their loop, and you can call it directly for a single model call. Calls made with a handler's context are traced in the run.

## Providers

Create a client for a provider. Each reads its API key from the environment unless you pass `apiKey`:

| Client | Key variable | Example `model` |
|---|---|---|
| `LM.openai()` | `OPENAI_API_KEY` | `'openai/gpt-6-luna'` |
| `LM.anthropic()` | `ANTHROPIC_API_KEY` | `'anthropic/claude-haiku-4-5-20251001'` |
| `LM.google()` | `GOOGLE_API_KEY` | `'google/gemini-2.5-flash'` |

`LM` also has clients for Azure OpenAI, Bedrock, Groq, Fireworks, OpenRouter, DeepSeek, Mistral, Together, xAI, Ollama, and others, for example `LM.groq()` and `LM.ollama()`. See [AI providers](/docs/integrations/ai-providers.md) for credentials.

## `lm.generate(request)`

```typescript
import { fn, LM } from '@agnt5/sdk';
import type { Context } from '@agnt5/sdk';

export const pickColor = fn('pick_color').run(async (ctx: Context) => {
  const response = await LM.openai().generate({
    model: 'openai/gpt-6-luna',
    systemPrompt: 'Reply in five words or fewer.',
    messages: [{ role: 'user', content: 'Name a primary color.' }],
  });
  return response.text;
});
```

| Request field | Description |
|---|---|
| `model` | The model, as `provider/model`. |
| `systemPrompt` | System instructions. |
| `messages` | The conversation: `{ role, content }` with role `'user'`, `'assistant'`, or `'system'`. |
| `prompt` | A stored [Prompt](/docs/build/prompts.md): `{ id, variables?, version? }`. Use it instead of `systemPrompt` and `messages`. |
| `config` | Generation options. See below. |

| Response field | Description |
|---|---|
| `text` | The model's reply. |
| `model` | The model that answered. |
| `finishReason` | Why generation stopped, as the provider reports it. |
| `usage` | `promptTokens`, `completionTokens`, `totalTokens`, and `cachedTokens`. Anthropic also reports `cacheCreationTokens`. |
| `toolCalls` | Tool calls the model made. |
| `structuredOutput` | The parsed object when you asked for JSON output. |

### `config`

| Option | Description |
|---|---|
| `responseFormat` | Ask for JSON: `{ formatType: 'json_schema', schemaName, schema, strict }`, where `schema` is a JSON Schema string. The parsed result is in `structuredOutput`. |
| `reasoningEffort` | `'none'`, `'minimal'`, `'low'`, `'medium'`, or `'high'`, for models that reason. |
| `cache` | [Prompt caching](/docs/build/prompt-caching.md): `true`, or a policy such as `{ ttl: '1h' }`. |

```typescript
const response = await LM.openai().generate({
  model: 'openai/gpt-6-luna',
  messages: [{ role: 'user', content: 'Give the capital of France.' }],
  config: {
    responseFormat: {
      formatType: 'json_schema',
      schemaName: 'capital',
      schema: JSON.stringify({
        type: 'object',
        properties: { city: { type: 'string' } },
        required: ['city'],
        additionalProperties: false,
      }),
      strict: true,
    },
  },
});
// response.structuredOutput → { city: 'Paris' }
```

## `lm.stream(request, callback)`

Streams the reply. The callback receives `delta` chunks with `content`, then one `completed` chunk with the full `response`:

```typescript
await LM.openai().stream(
  { model: 'openai/gpt-6-luna', messages: [{ role: 'user', content: 'Count from 1 to 5.' }] },
  (chunk) => {
    if (chunk.chunkType === 'delta') process.stdout.write(chunk.content ?? '');
    if (chunk.chunkType === 'completed') console.log('\n', chunk.response?.usage);
  },
);
```

## Next steps

* [Agents](/sdk/typescript/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.
