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.
Extends
Section titled “Extends”Properties
Section titled “Properties”agent:
object
Agent definition: name, model, instructions.
instructions
Section titled “instructions”instructions:
string
model:
string
name:
string
Inherited from
Section titled “Inherited from”callIndex
Section titled “callIndex”callIndex:
number
Which model call in this turn (0-based).
config
Section titled “config”config:
Record<string,unknown>
Middleware-specific configuration from the agent definition.
Inherited from
Section titled “Inherited from”history
Section titled “history”history:
Message[]
Canonical conversation history (append-only).
Inherited from
Section titled “Inherited from”input:
Message[]
Input messages for this turn.
Inherited from
Section titled “Inherited from”messages
Section titled “messages”messages:
Message[]
Mutable message array for this LLM call. Middleware can modify freely.
model:
string
Model identifier. Middleware can override via setModel().
output
Section titled “output”output:
string|null
Assistant’s final text output for this turn. null until the turn completes.
Inherited from
Section titled “Inherited from”sessionId
Section titled “sessionId”sessionId:
string
Unique session identifier.
Inherited from
Section titled “Inherited from”startedAt
Section titled “startedAt”startedAt:
number
Timestamp when this turn started.
Inherited from
Section titled “Inherited from”state:
Record<string,unknown>
Session state — typed fields with optional reducers, shared across all turns.
Inherited from
Section titled “Inherited from”toolDefs
Section titled “toolDefs”toolDefs:
object[]
Tool schemas sent to the LLM. Middleware can filter via removeTools().
description
Section titled “description”description:
string
jsonSchema
Section titled “jsonSchema”jsonSchema:
Record<string,unknown>
name:
string
turnId
Section titled “turnId”turnId:
string
Unique turn identifier.
Inherited from
Section titled “Inherited from”turnIndex
Section titled “turnIndex”turnIndex:
number
Turn number within this session (0-based).
Inherited from
Section titled “Inherited from”Methods
Section titled “Methods”abort()
Section titled “abort()”abort(
reason):never
Hard-stop the turn. Throws AbortError that unwinds the entire onion stack.
Parameters
Section titled “Parameters”reason
Section titled “reason”string
Returns
Section titled “Returns”never
Throws
Section titled “Throws”Inherited from
Section titled “Inherited from”addMessage()
Section titled “addMessage()”addMessage(
msg):void
Append a message to the messages array.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”void
addSystemMessage()
Section titled “addSystemMessage()”addSystemMessage(
text):void
Prepend a system message to the messages array.
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”void
emit()
Section titled “emit()”emit(
event):void
Emit a stream event to the consumer.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”registerTool()
Section titled “registerTool()”registerTool(
tool):void
Register a tool on the agent. Call in the agent hook before next().
Parameters
Section titled “Parameters”Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”removeTools()
Section titled “removeTools()”removeTools(…
names):void
Remove tools by name from this call’s tool definitions.
Parameters
Section titled “Parameters”…string[]
Returns
Section titled “Returns”void
setModel()
Section titled “setModel()”setModel(
model):void
Override the model for this call only.
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”void
skipCall()
Section titled “skipCall()”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.
Parameters
Section titled “Parameters”response
Section titled “response”Returns
Section titled “Returns”void