Context API
ctx.message
ctx.message represents the assistant message produced in the current turn.
When It Exists
ctx.message represents the assistant message generated during the current turn.
This value is only available in post-turn hooks that run after the model finishes generating a response.
During prompt construction (onContextBuild), ctx.message is always null.
It becomes populated when the host invokes hooks such as:
onTurnEndonToolCallonError
In these hooks, ctx.message refers to the assistant message that was just produced.
Shape
interface TurnMessage {
id: string
role: 'assistant'
content: string | null
finishReason?: 'stop' | 'length' | 'tool_calls' | 'error'
toolCalls?: ToolCall[]
}Fields
id– unique identifier of the assistant messagerole– always"assistant"content– textual content produced by the modelfinishReason– reason the model stopped generatingtoolCalls– tool calls issued during this turn (if any)
content may be null in cases such as:
- the model produced only tool calls
- generation failed
- the turn was aborted
Practical Use
Strategies typically use ctx.message in post-turn hooks to:
- skip post-processing after aborted or failed turns
- extract structured memory from generated text
- inspect tool calls produced during the turn
Example:
export async function onTurnEnd(ctx) {
if (!ctx.message) return
if (ctx.message.finishReason !== 'stop') {
return
}
const text = ctx.message.content
if (!text) return
// perform memory extraction or post-processing
}Notes
ctx.message represents only the assistant message generated in the current turn.
It does not expose earlier messages.
To access conversation history, use:
ctx.history