Skip to main content

RunContext

The RunContext object is available inside tool execute functions, hooks, guardrails, and dynamic instructions. It carries everything about the current run.

Methods

Example: Using RunContext in a tool

Example: Using RunContext in dynamic instructions


ChatMessage

Represents a single message in a conversation.

Content formats

Plain text — most common:
Multi-modal — images, audio, files:
Tool result — response to a tool call:

ContentPart

Multi-modal content is an array of ContentPart objects. Each part has a type discriminant.

TextPart

ImagePart

AudioPart

FilePart


ModelResponse

Returned by ModelProvider.generate(). Contains the full LLM response.

finishReason values


StreamChunk

Yielded by ModelProvider.stream() and agent.stream(). A discriminated union — check the type field.

Example: Processing a stream


TokenUsage

Token consumption breakdown from an LLM call.

RunOutput

The object returned by agent.run().

RunOpts

Per-run options passed to agent.run() or agent.stream(). All fields are optional.

Example


AgentHooks

Lifecycle hooks called during an agent run.

Example


LoopHooks

Per-roundtrip hooks for fine-grained control over the LLM loop. More granular than AgentHooks.

Example: Cost auto-stop


Guardrails

InputGuardrail

OutputGuardrail

GuardrailResult

A discriminated union:

Example


RetryConfig

Configuration for automatic retries on transient LLM API failures. Default retryable errors: HTTP 429 (rate limit), 5xx (server errors), ECONNRESET, ETIMEDOUT, ENOTFOUND, and messages containing “rate limit” or “overloaded”.

ApprovalConfig

Human-in-the-loop approval for tool calls.

SandboxConfig

Run tools in isolated subprocesses with resource limits.

ToolDef

The tool definition interface. Created with defineTool().

EventBus

Typed publish/subscribe event system for agent lifecycle events.

Example

Common events

See the full event list in the Events types source.

ReasoningConfig

Enable extended thinking / chain-of-thought for models that support it.

ContextCompactorConfig

Automatic context compaction to prevent context window overflow.

ToolResultLimitConfig

Prevent prompt token explosion from large tool results.