Overview
chat.inject() queues model messages for injection into the conversation. Messages are picked up at the start of the next turn or at the next prepareStep boundary (between tool-call steps).
This is the backend counterpart to pending messages — pending messages come from the user via the frontend, while chat.inject() comes from your task code.
Basic usage
Common pattern: defer + inject
The most powerful pattern combineschat.defer() (background work) with chat.inject() (inject results). Background work runs in parallel with the idle wait between turns, and results are injected before the next response.
Timing
- Turn completes,
onTurnCompletefires chat.defer()registers the background work- The run immediately starts waiting for the next message (no blocking)
- Background work completes,
chat.inject()queues the messages - User sends next message, turn starts
- Injected messages are appended before
run()executes - The LLM sees the injected context alongside the new user message
prepareStep boundary instead.
Example: self-review
A cheap model reviews the agent’s response after each turn and injects coaching for the next one. Uses Prompts for the review prompt andgenerateObject for structured output.
claude-haiku-4-5 (fast, cheap) in the background. If the user sends another message before it completes, the coaching is still injected — chat.inject() persists across the idle wait.
Other use cases
- RAG augmentation: After each turn, fetch relevant documents and inject them as context for the next response
- Safety checks: Run a moderation model on the response, inject warnings if issues are detected
- Fact-checking: Verify claims in the response using search tools, inject corrections
- Context enrichment: Look up user/account data based on what was discussed, inject it as system context
chat.defer standalone
chat.defer() is also useful on its own, without chat.inject(). Any work whose timing has no resume implication — analytics, audit logs, search-index writes, cache warming — can run in parallel with streaming instead of in the critical path. All deferred promises are awaited (with a 5s timeout) before onTurnComplete fires.
chat.defer() can be called from anywhere during a turn — hooks, run(), or nested helpers. All deferred promises are collected and awaited together before onTurnComplete.
How it differs from pending messages
API reference
chat.inject()
Messages are drained (consumed) when:
- A new turn starts — before
run()executes - A
prepareStepboundary is reached — between tool-call steps during streaming
chat.inject() writes to an in-memory queue in the current process. It works from any code running in the same task — lifecycle hooks, deferred work, tool execute functions, etc. It does not work from subtasks or other runs.
