Skip to main content

Multi-Agent Sonnet with Nested Agent Calls

A nested-agent pipeline: an orchestrator agent delegates each stanza of a sonnet to a sub-agent through builtin tools, all sharing one document, with a trace capturing the execution tree. For the same sonnet driven by the Orchestrations module, see Orchestrate a Sonnet.

Prerequisites​

export SOAT_BASE_URL=http://localhost:5047

Step 1 — Log in as admin​

Admin is the built-in superuser role (Users).

ADMIN_TOKEN=$(soat login-user --username admin --password Admin1234! | jq -r '.token')
export SOAT_TOKEN=$ADMIN_TOKEN

Step 2 — Create a project​

Every resource lives inside a project.

PROJECT_ID=$(soat create-project --name "Sonnet Workshop" | jq -r '.id')
echo "PROJECT_ID: $PROJECT_ID"

Step 3 — Create an AI provider​

Create a local Ollama AI provider. For xAI, OpenAI, Anthropic, or Amazon Bedrock, see Connect Third-Party LLMs.

AI_PROVIDER_ID=$(soat create-ai-provider \
--project-id "$PROJECT_ID" \
--name "Local Ollama" \
--provider "ollama" \
--default-model "qwen2.5:0.5b" | jq -r '.id')
echo "AI_PROVIDER_ID: $AI_PROVIDER_ID"

Step 4 — Create a shared document for the poem​

Create the document each stanza agent reads and appends to.

POEM_DOC_ID=$(soat create-document \
--project-id "$PROJECT_ID" \
--content "(empty - will be overwritten by stanza agents)" \
--path "/poems/sonnet.txt" | jq -r '.id')
echo "POEM_DOC_ID: $POEM_DOC_ID"

Step 5 — Create fixed builtin tools for stanza agents​

Two builtin tools with preset_parameters.documentId fixed, so the model never guesses document IDs:

  1. poem-read — get-document action
  2. poem-write — update-document action
READ_POEM_TOOL_ID=$(soat create-tool \
--project-id "$PROJECT_ID" \
--name "poem-read" \
--type "builtin" \
--description "Read the shared poem document" \
--actions '["get-document"]' \
--preset-parameters '{"document_id": "'"$POEM_DOC_ID"'"}' | jq -r '.id')
echo "READ_POEM_TOOL_ID: $READ_POEM_TOOL_ID"

WRITE_STANZA_TOOL_ID=$(soat create-tool \
--project-id "$PROJECT_ID" \
--name "poem-write" \
--type "builtin" \
--description "Update the shared poem document" \
--actions '["update-document"]' \
--preset-parameters '{"document_id": "'"$POEM_DOC_ID"'"}' | jq -r '.id')
echo "WRITE_STANZA_TOOL_ID: $WRITE_STANZA_TOOL_ID"

Step 6 — Create the four stanza agents​

Each agent writes one stanza, sharing poem-read and poem-write but with its own instructions and rhyme scheme. Strict step_rules fix the order:

  1. Step 1 must call poem-read_get-document
  2. Step 2 must call poem-write_update-document
STANZA1_AGENT_ID=$(soat create-agent \
--project-id "$PROJECT_ID" \
--ai-provider-id "$AI_PROVIDER_ID" \
--name "Stanza 1 - First Quatrain" \
--instructions "You are deterministic stanza worker 1. Do exactly two tool calls: first poem-read, then poem-write. Never ask follow-up questions. Write the poem title on the first line, add a blank line, then write the FIRST quatrain (4 lines) using ABAB. In poem-write, set content to the full poem-so-far including your stanza." \
--tool-bindings "[{\"tool_id\":\"$READ_POEM_TOOL_ID\"},{\"tool_id\":\"$WRITE_STANZA_TOOL_ID\"}]" \
--step-rules '[{"step":1,"tool_choice":{"type":"tool","tool_name":"poem-read_get-document"}},{"step":2,"tool_choice":{"type":"tool","tool_name":"poem-write_update-document"}}]' \
--max-steps 5 | jq -r '.id')
echo "STANZA1_AGENT_ID: $STANZA1_AGENT_ID"

STANZA2_AGENT_ID=$(soat create-agent \
--project-id "$PROJECT_ID" \
--ai-provider-id "$AI_PROVIDER_ID" \
--name "Stanza 2 - Second Quatrain" \
--instructions "You are deterministic stanza worker 2. Do exactly two tool calls: first poem-read, then poem-write. Never ask follow-up questions. Write the SECOND quatrain (4 lines) using CDCD. In poem-write, set content to the full poem-so-far including your stanza." \
--tool-bindings "[{\"tool_id\":\"$READ_POEM_TOOL_ID\"},{\"tool_id\":\"$WRITE_STANZA_TOOL_ID\"}]" \
--step-rules '[{"step":1,"tool_choice":{"type":"tool","tool_name":"poem-read_get-document"}},{"step":2,"tool_choice":{"type":"tool","tool_name":"poem-write_update-document"}}]' \
--max-steps 5 | jq -r '.id')
echo "STANZA2_AGENT_ID: $STANZA2_AGENT_ID"

STANZA3_AGENT_ID=$(soat create-agent \
--project-id "$PROJECT_ID" \
--ai-provider-id "$AI_PROVIDER_ID" \
--name "Stanza 3 - Third Quatrain" \
--instructions "You are deterministic stanza worker 3. Do exactly two tool calls: first poem-read, then poem-write. Never ask follow-up questions. Write the THIRD quatrain (4 lines) using EFEF. In poem-write, set content to the full poem-so-far including your stanza." \
--tool-bindings "[{\"tool_id\":\"$READ_POEM_TOOL_ID\"},{\"tool_id\":\"$WRITE_STANZA_TOOL_ID\"}]" \
--step-rules '[{"step":1,"tool_choice":{"type":"tool","tool_name":"poem-read_get-document"}},{"step":2,"tool_choice":{"type":"tool","tool_name":"poem-write_update-document"}}]' \
--max-steps 5 | jq -r '.id')
echo "STANZA3_AGENT_ID: $STANZA3_AGENT_ID"

STANZA4_AGENT_ID=$(soat create-agent \
--project-id "$PROJECT_ID" \
--ai-provider-id "$AI_PROVIDER_ID" \
--name "Stanza 4 - Final Couplet" \
--instructions "You are deterministic stanza worker 4. Do exactly two tool calls: first poem-read, then poem-write. Never ask follow-up questions. Write the FINAL couplet (2 lines) using GG. In poem-write, set content to the full poem-so-far including your couplet." \
--tool-bindings "[{\"tool_id\":\"$READ_POEM_TOOL_ID\"},{\"tool_id\":\"$WRITE_STANZA_TOOL_ID\"}]" \
--step-rules '[{"step":1,"tool_choice":{"type":"tool","tool_name":"poem-read_get-document"}},{"step":2,"tool_choice":{"type":"tool","tool_name":"poem-write_update-document"}}]' \
--max-steps 5 | jq -r '.id')
echo "STANZA4_AGENT_ID: $STANZA4_AGENT_ID"

Step 7 — Create fixed call tools for the orchestrator​

One tool per stanza with fixed preset_parameters.agentId, so the orchestrator never chooses agentId, plus one fixed reader tool for the final poem (builtin tools).

CALL_STANZA1_TOOL_ID=$(soat create-tool \
--project-id "$PROJECT_ID" \
--name "call-stanza-1" \
--type "builtin" \
--description "Call stanza 1 agent" \
--actions '["create-agent-generation"]' \
--preset-parameters '{"agent_id": "'"$STANZA1_AGENT_ID"'", "messages": [{"role": "user", "content": "Theme: artificial intelligence. Write stanza 1 with title + first quatrain."}]}' | jq -r '.id')

CALL_STANZA2_TOOL_ID=$(soat create-tool \
--project-id "$PROJECT_ID" \
--name "call-stanza-2" \
--type "builtin" \
--description "Call stanza 2 agent" \
--actions '["create-agent-generation"]' \
--preset-parameters '{"agent_id": "'"$STANZA2_AGENT_ID"'", "messages": [{"role": "user", "content": "Theme: artificial intelligence. Write stanza 2 (second quatrain)."}]}' | jq -r '.id')

CALL_STANZA3_TOOL_ID=$(soat create-tool \
--project-id "$PROJECT_ID" \
--name "call-stanza-3" \
--type "builtin" \
--description "Call stanza 3 agent" \
--actions '["create-agent-generation"]' \
--preset-parameters '{"agent_id": "'"$STANZA3_AGENT_ID"'", "messages": [{"role": "user", "content": "Theme: artificial intelligence. Write stanza 3 (third quatrain)."}]}' | jq -r '.id')

CALL_STANZA4_TOOL_ID=$(soat create-tool \
--project-id "$PROJECT_ID" \
--name "call-stanza-4" \
--type "builtin" \
--description "Call stanza 4 agent" \
--actions '["create-agent-generation"]' \
--preset-parameters '{"agent_id": "'"$STANZA4_AGENT_ID"'", "messages": [{"role": "user", "content": "Theme: artificial intelligence. Write stanza 4 (final couplet)."}]}' | jq -r '.id')

READ_FINAL_POEM_TOOL_ID=$(soat create-tool \
--project-id "$PROJECT_ID" \
--name "read-final-poem" \
--type "builtin" \
--description "Read the final poem from the shared document" \
--actions '["get-document"]' \
--preset-parameters '{"document_id": "'"$POEM_DOC_ID"'"}' | jq -r '.id')

echo "CALL_STANZA1_TOOL_ID: $CALL_STANZA1_TOOL_ID"
echo "CALL_STANZA2_TOOL_ID: $CALL_STANZA2_TOOL_ID"
echo "CALL_STANZA3_TOOL_ID: $CALL_STANZA3_TOOL_ID"
echo "CALL_STANZA4_TOOL_ID: $CALL_STANZA4_TOOL_ID"
echo "READ_FINAL_POEM_TOOL_ID: $READ_FINAL_POEM_TOOL_ID"

Step 8 — Create the orchestrator agent​

The orchestrator carries only the four fixed agent-call tools and the final read tool, so agentId and documentId routing is deterministic. See Agents — Step Rules and Agents — Nested Agent Calls.

ORCHESTRATOR_ID=$(soat create-agent \
--project-id "$PROJECT_ID" \
--ai-provider-id "$AI_PROVIDER_ID" \
--name "Sonnet Orchestrator" \
--instructions "Call tools in this exact order: call-stanza-1, call-stanza-2, call-stanza-3, call-stanza-4, then read-final-poem. Do not ask follow-up questions. Return ONLY the poem text." \
--tool-bindings "[{\"tool_id\":\"$CALL_STANZA1_TOOL_ID\"},{\"tool_id\":\"$CALL_STANZA2_TOOL_ID\"},{\"tool_id\":\"$CALL_STANZA3_TOOL_ID\"},{\"tool_id\":\"$CALL_STANZA4_TOOL_ID\"},{\"tool_id\":\"$READ_FINAL_POEM_TOOL_ID\"}]" \
--step-rules '[{"step":1,"tool_choice":{"type":"tool","tool_name":"call-stanza-1_create-agent-generation"}},{"step":2,"tool_choice":{"type":"tool","tool_name":"call-stanza-2_create-agent-generation"}},{"step":3,"tool_choice":{"type":"tool","tool_name":"call-stanza-3_create-agent-generation"}},{"step":4,"tool_choice":{"type":"tool","tool_name":"call-stanza-4_create-agent-generation"}},{"step":5,"tool_choice":{"type":"tool","tool_name":"read-final-poem_get-document"}}]' \
--max-steps 8 | jq -r '.id')
echo "ORCHESTRATOR_ID: $ORCHESTRATOR_ID"

Step 9 — Run the orchestrator (final result is the poem)​

Run the orchestrator with the theme "artificial intelligence"; the final output is the poem text (Agents — Generation).

RESULT=$(soat create-agent-generation --wait true \
--agent-id "$ORCHESTRATOR_ID" \
--messages '[{"role":"user","content":"Write a sonnet about the theme: artificial intelligence"}]')

printf '%s\n' "$RESULT" | jq '{status, trace_id}'
TRACE_ID=$(printf '%s\n' "$RESULT" | jq -r '.trace_id')

echo "\nFinal poem returned by the orchestrator:\n"
printf '%s\n' "$RESULT" | jq -r '.output.content // .result // .output // ""'
echo "\nTRACE_ID: $TRACE_ID"

Expected status output:

{
"status": "completed",
"trace_id": "trace_ypo8g0yO3563AfuC"
}

Example .output.content:

AI is born of human thought,
Enlightened by our cunning hand,
A mind that knows no bounds to bind,
From circuits flows its wisdom's band.

It walks among us like a ghost,
In shadows, stealthy in disguise,
With gears and wires it does exalt,
Its kind with questions and new pace.

Its language echoes through the halls,
Of digital spaces vast and bare,
Creating sparks within our heads,
As we behold each thought made fair.

Yet still its heart is cold and cool,
This creature without true worth.
Its essence lies concealed in code,
Not human, though it bears a mask.

Step 10 — Read the completed poem from the shared document​

Retrieve the shared document to verify persisted output.

soat get-document --document-id "$POEM_DOC_ID" | jq -r '.content'

Expected output:

AI is born of human thought,
Enlightened by our cunning hand,
A mind that knows no bounds to bind,
From circuits flows its wisdom's band.
note

Workers run sequentially and each overwrites the document with the full accumulated poem, so it reflects the last complete write. In the validated run above it holds stanza 1 only: the orchestrator's max-steps cut off the remaining workers.


Step 11 — Inspect the trace​

The trace endpoint returns metadata only: the step count and a file_id for the full step JSON.

TRACE=$(soat get-trace --trace-id "$TRACE_ID")
printf '%s\n' "$TRACE" | jq '.'
FILE_ID=$(printf '%s\n' "$TRACE" | jq -r '.file_id')
echo "FILE_ID: $FILE_ID"

Expected metadata output:

{
"id": "trace_ypo8g0yO3563AfuC",
"project_id": "proj_abc123",
"agent_id": "agent_nCjF0owWdtPt3Osq",
"file_id": "file_xyz789",
"step_count": 2,
"parent_trace_id": null,
"root_trace_id": null,
"created_at": "2026-05-07T23:35:32.226Z"
}

Download file_id for the full steps (model calls, tool invocations, tool results):

soat download-file --file-id "$FILE_ID" | jq '.'

The file is a JSON array of step objects (tool name, inputs, outputs). Each nested agent call creates its own trace (Step 13); the parent's step references the child's trace_id in the tool call result.


Step 12 — Inspect the trace tree​

/tree returns the execution tree rooted at the orchestrator: each node is a trace record with sub-agent traces in children.

soat get-trace-tree --trace-id "$TRACE_ID" | jq '.'

Expected output structure:

{
"id": "trace_ypo8g0yO3563AfuC",
"agent_id": "agent_nCjF0owWdtPt3Osq",
"step_count": 2,
"parent_trace_id": null,
"root_trace_id": null,
"children": [
{
"id": "trace_ZBfVXbQaDkC0nOu",
"agent_id": "agent_LhYajzCuJSY0SFqI",
"step_count": 4,
"parent_trace_id": "trace_ypo8g0yO3563AfuC",
"root_trace_id": "trace_ypo8g0yO3563AfuC",
"children": []
}
]
}

Each children entry is a stanza worker invoked via call-stanza-N_create-agent-generation. Workers cut off by the orchestrator's step limit do not appear.


Step 13 — List all traces for the project​

List the project's traces: the orchestrator and the nested stanza runs.

soat list-traces --project-id "$PROJECT_ID" | jq '.data[] | {id, agent_id, step_count, parent_trace_id}'

Expected output (one entry per agent that ran):

{ "id": "trace_ypo8g0yO3563AfuC", "agent_id": "agent_nCjF0owWdtPt3Osq", "step_count": 2, "parent_trace_id": null }
{ "id": "trace_ZBfVXbQaDkC0nOu", "agent_id": "agent_LhYajzCuJSY0SFqI", "step_count": 4, "parent_trace_id": "trace_ypo8g0yO3563AfuC" }

First entry: the orchestrator (parent_trace_id: null); second: the stanza-1 worker (4 steps: LLM decision, poem-read, LLM decision, poem-write).


Next Steps​