Skip to main content

What This Example Shows

  • How to pass prior turns to an agent using the history field 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. The history 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, so history is omitted. The agent answers from a clean slate.
The agent greets the customer back and likely asks how it can help — standard opening turn, nothing to remember yet.
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 into history, then send the next user message as the new task.
Because turn 1 is in 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 to history, 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.
By turn 3 the agent has the full conversational arc — greeting, missing-order complaint, pivot to refund — and can route the customer to the refund flow while citing the original order issue as the reason. Without 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.
Common mistake — losing context by overwriting instead of appending. The most frequent bug we see is sending only the latest user/assistant pair as history, or rebuilding history from scratch each turn. The agent then loses everything before that pair. history is a transcript of the whole conversation so far; you append to it every turn, you do not replace it. If your bot suddenly “forgets” what the user said three messages ago, this is almost always the cause.
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.
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.
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