Skip to content

ModelContext

Context available during the model hook (wraps one LLM call).

Extends TurnContext with mutable messages, model selection, tool definitions, and short-circuit methods.

messages is a mutable copy prepared for this specific LLM call — middleware can truncate, inject, or reorder without affecting SessionContext.history.

agent: object

Agent definition: name, model, instructions.

instructions: string

model: string

name: string

TurnContext.agent


callIndex: number

Which model call in this turn (0-based).


config: Record<string, unknown>

Middleware-specific configuration from the agent definition.

TurnContext.config


history: Message[]

Canonical conversation history (append-only).

TurnContext.history


input: Message[]

Input messages for this turn.

TurnContext.input


messages: Message[]

Mutable message array for this LLM call. Middleware can modify freely.


model: string

Model identifier. Middleware can override via setModel().


output: string | null

Assistant’s final text output for this turn. null until the turn completes.

TurnContext.output


sessionId: string

Unique session identifier.

TurnContext.sessionId


startedAt: number

Timestamp when this turn started.

TurnContext.startedAt


state: Record<string, unknown>

Session state — typed fields with optional reducers, shared across all turns.

TurnContext.state


toolDefs: object[]

Tool schemas sent to the LLM. Middleware can filter via removeTools().

description: string

jsonSchema: Record<string, unknown>

name: string


turnId: string

Unique turn identifier.

TurnContext.turnId


turnIndex: number

Turn number within this session (0-based).

TurnContext.turnIndex

abort(reason): never

Hard-stop the turn. Throws AbortError that unwinds the entire onion stack.

string

never

TurnContext.abort


addMessage(msg): void

Append a message to the messages array.

Message

void


addSystemMessage(text): void

Prepend a system message to the messages array.

string

void


emit(event): void

Emit a stream event to the consumer.

StreamEvent

void

TurnContext.emit


registerTool(tool): void

Register a tool on the agent. Call in the agent hook before next().

Tool

void

TurnContext.registerTool


removeTools(…names): void

Remove tools by name from this call’s tool definitions.

string[]

void


setModel(model): void

Override the model for this call only.

string

void


skipCall(response): void

Skip the LLM call entirely and return a synthetic response. Used for caching — the cached response is returned without calling the provider.

ModelResponse

void