What This Example Shows
- How to pass prior turns to an agent using the
historyfield on/v1/agent/completions - The exact
{role, content}message shape the API expects - How to thread the assistant’s reply back into the next request to keep context
- A worked support-chatbot loop across three turns
- The most common mistake — silently dropping context — and how to avoid it
The Swarms API is stateless. Each call to
/v1/agent/completions starts a fresh agent. To get multi-turn behavior, you carry the conversation: send every prior user and assistant message back on the history field of the next request.Why This Matters
Most real products — support chatbots, multi-turn research assistants, onboarding flows, interactive analysts — only feel useful once the agent remembers earlier turns. Without that, your user has to re-state their problem on every message. Thehistory parameter is the single foundational primitive that turns a one-shot completion into a real conversation, and threading it correctly is the difference between an agent that feels alive and one that feels amnesiac.
Step 1: Setup
Step 2: Turn 1 — Greeting (No History)
The first turn has no prior context, sohistory is omitted. The agent answers from a clean slate.
The Swarms API accepts
history as a list of {role, content} dicts. Valid roles are "user" and "assistant". The system_prompt lives in agent_config — do NOT prepend a {"role": "system", ...} message into history.Step 3: Turn 2 — “Where is my order?”
Append the previous user message and assistant reply intohistory, then send the next user message as the new task.
history, the agent treats this as a continuation of an ongoing chat rather than a brand-new session — it doesn’t re-greet, and it can reference the earlier hello if relevant.
Step 4: Turn 3 — “Actually, I want a refund”
The customer pivots. Append turn 2 tohistory, then ask for a refund. With the full thread in context, the agent connects the refund request to the missing-order complaint from turn 2 instead of treating it as a fresh, unrelated request.
history, it would ask “refund for what?” and the customer would have to start over.
Step 5: Wrap It in a Reusable Chat Loop
The full pattern collapses into a tiny REPL you can drop into a CLI, a webhook handler, or a websocket session.Should I include the latest user `task` inside `history`?
Should I include the latest user `task` inside `history`?
No. Send the new turn as
task and put only the prior turns in history. The API stitches them together internally. Putting the current question in both places will duplicate it in the model’s view.How long can `history` get before I should trim?
How long can `history` get before I should trim?
It scales with the model’s context window. For long sessions, periodically summarize older turns into a single
assistant message and keep only the last N raw turns verbatim — same pattern as any other chat application.Can I persist history across processes?
Can I persist history across processes?
Yes.
history is just JSON. Store it in your database keyed by session/user ID and rehydrate it on the next request. The Swarms API holds no server-side session state for /v1/agent/completions.Next Steps
- Single Agent Overview — the full
agent_configsurface, including tools and vision - Streaming Responses — stream the assistant’s reply token-by-token while still threading history
- Sub-Agent Delegation — let a conversational agent hand off specialist sub-tasks mid-thread