---
related_nodes: [ai_conversation, tool_provider, spawn_agent_tool]
---

# AI Agent

Runs an LLM against workflow input. Supports tools, structured output, images, files, and intermediate status events.

Memory input is structural. Wire `memory_recall.output` into `ai_agent.memory`; the node formats those memory items into prompt context at runtime.

## Required model setup

Model selection is one object field:

```json
{ "model": { "connection_id": "<connection_id from list_models>", "model_id": "glm-5.1" } }
```

Use `list_models`. Select `model.connection_id`, then select `model.model_id` from that connection's `models[]`.

## Inputs

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `input` | Any | Yes | Node input for interpolating into the user prompt via template syntax. |
| `memory` | Array or null | No | Structured memory items. Wire `memory_recall.output` here. |
| `schema` | Object | No | JSON Schema for structured output. When provided, the AI will output valid JSON matching this schema. |
| `images` | Array | No | Optional array of images. Accepts URLs, object descriptors, NodeData::Binary, or `{binary, mime_type, filename}` payloads. Binary payloads are converted to inline data URLs for the provider — no Upload Media required. Default: `[]`. |
| `files` | Array | No | Optional array of files. Accepts URLs, object descriptors, NodeData::Binary, or `{binary, mime_type, filename}` payloads. Binary payloads are converted to inline data URLs for the provider — no Upload Media required. Default: `[]`. |

### Structured output with tools

On models with native structured output, `tools` and `schema` can be wired together. Tool-calling turns do not need a schema payload; the final answer supplies it through `structured`. Invalid JSON in an answering turn still fails the run.

## Outputs

| Name | Type | Description |
|------|------|-------------|
| `response` | String | The LLM response. |
| `artifacts` | Array | Generated file artifacts from the LLM. |
| `metrics` | Object | Final model identity, cumulative `{prompt, completion, total}` token counts, nullable cost in micro-USD, and latest `{used, limit}` context-window occupancy. |
| `structured` | Any | Parsed JSON output when a schema is provided. Also carries the tool call that ended the run when a [Yield Tool](yield_tool.md) was called — a yielded call is the run's final message, so it leaves through this socket. Null when there is neither. |
| `structured_error` | Object | Error details when structured output parsing/validation fails. Null on success. |
| `intermediate` | Object | Triggers an embedded body during agent execution. The final socket value is `null`; the body receives `{event}`. Branch on `event.kind`: `assistant_message`, `tool_call`, `tool_result`, `response_finished`, `transcript_entry_finished`, `run_finished`. Token deltas are deliberately excluded — the body is a nested run per event. See [AI Conversation](ai_conversation.md) for payload shapes. |

`metrics` has this shape:

```json
{
  "model": { "provider": "anthropic", "id": "claude-sonnet-4-6" },
  "tokens": { "prompt": 18000, "completion": 1200, "total": 19200 },
  "cost": { "microusd": 8250, "currency": "USD", "kind": "estimated" },
  "context": { "used": 18000, "limit": 200000 }
}
```

`cost` is `null` when catalog pricing is unavailable. Context occupancy describes the latest model turn; token and cost values cover the complete node run. Calculate context percentage as `context.used / context.limit` when needed.

## Configuration

| Field | Description |
|---|---|
| `model` | Object containing `connection_id` and `model_id`. Select `model.connection_id`, then select `model.model_id` from that connection's `models[]` returned by `list_models`. |
| `system_prompt` | System instructions. |
| `agent_base` | Boolean, default off. Puts the Agent Base beneath `system_prompt`. See [Agent Base](#agent-base). |
| `user_prompt` | User prompt with template support. |
| `max_tokens` | Optional maximum output token cap. Disable/unset to use the provider/model maximum. Explicit low caps may truncate structured output or tool calls. |
| `temperature` | Optional sampling temperature (0-2). Off uses the model's own value. |
| `top_p` | Optional nucleus sampling top-p (0-1). Off uses the model's own value. |
| `top_k` | Optional top-k sampling limit (1 or more). Off uses the model's own value. |
| `max_turns` | Maximum tool-use turns. |

### Agent Base

`agent_base` sends working principles beneath `system_prompt`: finish the whole task, keep to the requested scope, adjust after a failed tool call instead of repeating it, and report only what it observed. Where the two differ, `system_prompt` wins. It is off by default, and a workflow saved without the field loads with it off.

## Media Input

The `images` and `files` sockets accept inline binary data from upstream nodes (Read Media, ProcessImage, Telegram Download, etc.). When `NodeData::Binary` or `{binary}` payloads are received, the node converts them to inline data URLs (`data:mime/type;base64,...`) and passes them directly to the provider. No Upload Media step is required for binary-feed chains.

For persisted media, provide a `{object_id: "..."}` reference or a direct URL.

## Common fixes

- Invalid model: call `list_models`; select `model.connection_id`, then select `model.model_id` from that connection's `models[]`.
- Missing auth: call `list_models`, then select a connection with usable credentials.
