Cortex · Prompt and contextPer-step control (
Per-step control (prepareStep)
prepareStep is called before every call to the model, with the step’s number, the messages so far and the usage. It may add messages (inject), which stay in the transcript, narrow the tools by name (activeTools; respond stays), set toolChoice or switch the model (llm). The model’s two turns are scripted; no model is called.
const desk = defineAgent({
id: 'desk',
instructions: 'Answer the member in one sentence.',
tools: [seatsLeft],
prepareStep: ({ step }) =>
step === 1
? undefined
: { activeTools: [], inject: [{ role: 'system', content: 'Answer now, from what the tool said.' }] },
});1 calls seatsLeft {"night":"2026-10-24"}
2 writes {"response":"113 seats are left on Saturday."}{
sent: [
{
step: 1,
tools: ['seatsLeft', 'respond'],
last: [
'system FINISH PROTOCOL: To finish, call the `respond` tool …',
'user How many seats are left on Saturday?',
],
},
{
step: 2,
tools: ['respond'],
last: [
'tool {"night":"2026-10-24","left":113}',
'system Answer now, from what the tool said.',
],
},
],
result: {
ok: true,
output: { response: '113 seats are left on Saturday.' },
},
}Runs in this page on the installed package. The input and the expected output are defined by the example.