How a Conversational AI Agent Works Inside
The 6 stages of a conversation turn in OpenClaw — with real latency, cost per conversation and the 4 lines of defence against hallucination.
Equipe OpenClaw · Time de Engenharia & Produto
A Equipe OpenClaw é formada por engenheiros, designers e especialistas em IA dedicados a construir a melhor plataforma de agentes conversacionais para negócios brasileiros. Combinamos expertise…
How Does a Conversational AI Agent Work Inside (OpenClaw Architecture)
How does a conversational AI agent work in practice, turn by turn? This post opens the black box of OpenClaw: from the moment the client's message arrives on WhatsApp to the text the agent writes back. It will be technical. Worth it if you decide to architect a product, if you're going to buy a solution and want to evaluate the foundation, or if you enjoy knowing what's happening behind the conversation.
TL;DR: each turn goes through 6 stages — ingest, resolve context, select skills, decide next action, execute with guard-rails, persist memory. The entire cycle runs in <seconds on the Cloudflare edge, without a fixed server.
Why the Architecture Matters
A conversational agent that seems to work in a demo but breaks in production generally has one of these 4 problems:
- High latency — client waits 8 seconds for a response, conversation dies.
- Uncontrolled hallucination — agent invents price, time, policy.
- Lost context — client comes back after 2 days and agent "forgets" everything.
- Uncontrolled cost — each long conversation fills the prompt and you pay a fortune in tokens.
The 4 are architecture choices, not model limitations. OpenClaw was built to avoid the 4 — and the path to understanding is to look at the cycle of a turn.
The Cycle of a Turn (6 Stages)
Imagine the client has just sent the message "I want to book for Saturday morning". What happens between the "received" and the agent's response?
Stage 1 — Ingest (edge worker, <ms)
The WhatsApp message arrives via webhook from Meta directly into a Cloudflare Worker at the nearest point of presence (PoP) geographically. In South Africa, this means Johannesburg or Cape Town, network latency <0ms.
The worker does three things:
- Validates the webhook signature (HMAC against the WABA secret).
- Identifies the tenant by the recipient's phone number (multi-tenant by
to_number). - Normalizes the payload — audio becomes transcription, image becomes description, location becomes
{lat,lng}, text stays as is.
At the end of stage 1, you have an object {tenant_id, conversation_id, user_message} ready for the next step.
Stage 2 — Resolve Context (D1 + KV, ~80ms)
The agent needs 3 pieces of context before deciding:
- Conversation history (D1 database).
- User profile (D1 database).
- Tenant settings (D1 database).
The agent fetches these 3 pieces of context from the D1 database and stores them in memory.
Stage 3 — Select Skills (D2, ~20ms)
The agent selects the relevant skills from the D2 database based on the user's message and the conversation history.
Stage 4 — Decide Next Action (D3, ~20ms)
The agent decides the next action based on the selected skills and the conversation history.
Stage 5 — Execute with Guard-Rails (D4, ~20ms)
The agent executes the next action with guard-rails to prevent uncontrolled hallucination.
Stage 6 — Persist Memory (D5, ~20ms)
The agent persists the memory of the conversation in the D5 database.
The entire cycle runs in <seconds on the Cloudflare edge, without a fixed server.
- Recente Geskiedenis van die gesprek (laaste N omwentelinge wat relevant is).
- Lange-termyn Onthou van die kliënt (voorkeure, geskiedenis van aankope, notas).
- Toestand van die Agent (persoon, vaardighede wat ingesluit is, reëls).
Almal kom van D1 (SQLite verspreiding van Cloudflare). D1 vervang tradisionele Postgres/Mongo - geen server om te onderhou, toegang in minste ms vanuit die werker, multi-tenant deur tenant_id.
Kluiswoord: ons laai die gesprek nie heeltemal in die aanvraag. Die Memory Manager v2 van OpenClaw (beskryf in ons inligtingsteken) selekteer slegs die omwentelinge wat relevant is vir die huidige omwenteling (laaste N + N van hoë relevansie semantiek).
Stadium 3 - Vaardigheidseleksie (beleidstelsel, ~20ms)
Elke agent het 'n set vaardighede beskikbaar - funksies wat hy kan aanroep. Voorbeelde: consulteer_kalender, maak_event, generer_link_betaling, consulteer_bestelling, roep_humane.
Gegee die boodskap "Ek wil myself aanmeld vir Saterdagoggend", die beleidstelsel filter:
- Vaardighede wat kompatibel is met die gedetecteerde doel (agendering).
- Vaardighede wat toegelaat word vir hierdie fase van die gesprek (nie elke vaardigheid is altyd beskikbaar).
- Vaardighede wat deur hierdie huurder ingesluit is (kalender sal slegs verskyn as die huurder dit geïntegreer het).
In die einde het jy 'n klein subset van vaardighede wat na die model gestuur word - nie die 50 moontlike, maar net die 4 wat sin maak hier. Dit verlaag die kans dat die model 'n verkeerde vaardigheid aanroep.
Stadium 4 - Besluit (LLM-aanroep, 400-1200ms)
Die model is nou aan die beurt. OpenClaw maak 'n enkele aanroep na 'n LLM van die voorste (Anthropic Claude, OpenAI GPT, Google Gemini - konfigureerbaar deur huurder) met:
- Sisteem-aanvraag = persoon van die agent + reëls + vaardighede wat beskikbaar is.
- Geskiedenis = omwentelinge wat geselekteer is in stadium 2.
- Gebruiker se boodskap = boodskap van die huidige omwenteling.
Die model antwoord een van twee dinge:
- Laaste antwoord (tekst direk na die kliënt).
- Tool-aanroep (aanvraag om 'n spesifieke vaardigheid uit te voer met parameters).
In die voorbeeld "Ek wil myself aanmeld vir Saterdagoggend", die model tipies terugkeer:
{
"tool": "consulteer_kalender",
"args": { "date_range": "2026-04-19 06:00 tot 12:00" }
}
Stadium 5 - Uitvoering met veiligheidsdraad (variabel, ~100-500ms)
Die vaardigheid nie loop in die model. Dit loop in ons kode, wat:
{
"tool": "consulteer_kalender",
"args": { "date_range": "2026-04-19 06:00 tot 12:00" }
}
- Valideer parameters (is date_range in the correct format? is it within the tenant's rules?).
- Check permission (does this agent have the right to consult this calendar?).
- Execute the call (Google Calendar API in this case).
- Return structured result to the model.
Why does this matter? Because the model never fabricates the result. If the calendar returns [10h, 11h], that's exactly what goes to the next call. If the skill fails, the model knows it failed. Zero risk of the agent "inventing" that it has a 9h appointment when it doesn't.
For cases involving sensitive information (price, deadline, client name), the pipeline forces tool call — it doesn't let the model respond from its own "knowledge". This eliminates the most common hallucination class in commercial agents.
Stage 6 — Response and persistence (~50ms)
With the skill result in hand, the model makes the second call — now to form the final response to the client. Ex:
"I have Saturday at 10h and 11h. Which one do you prefer?"
Parallelly, the worker:
- Sends the message back through the WhatsApp API.
- Persists the entire turn (user + assistant + tool calls + duration) in D1.
- Updates long-term memory if the turn produced new fact (ex: "client prefers Saturday").
- Emits observability event (latency metric, token cost, scaling rate).
Everything runs in parallel. Persistence does not block sending the message — the client doesn't wait for D1.
Where is the defense against hallucination
An agent that hallucinates in production loses trust quickly. The OpenClaw has 4 lines of defense:
- Forced source-of-truth. Factual data (price, time, name) always comes from the skill, never from the model alone.
- Double verification on sensitive data. Appointment is confirmed with the client before persisting. Payment is confirmed before releasing access.
- Explicit negative rules. Persona of each agent includes "never invent X, Y, Z" — the model obeys.
- Fallback to human. When no skill covers the question, the agent says
"let me check with the team"and opens a ticket — it doesn't guess.
In audits we've done in the last 6 months (real conversations manually reviewed), the factual hallucination rate was below 0.3% of turns — and almost all cases were due to config (tenant forgot to enable relevant skill), not model error.
Cost per conversation
Arkhitekture goeie is onsigbaar tot jy kyk na die faktuur. Gegee dat elke beurt 1-2 oproepe na LLM + lookups in D1 maak, die gemiddelde koste per gesprek voltooi (10-15 beurte) kom uit:
- 1-2 LLM-aanroep + lookups in D1 per beurt
- 10-15 beurte per gesprek
- Gemiddelde koste per gesprek: [bereken de kosten](bereken de kosten)
Koste per gesprek
- 1-2 LLM-aanroep + lookups in D1 per beurt
- 10-15 beurte per gesprek
- Gemiddelde koste per gesprek: [bereken de kosten](bereken de kosten)
# Voorbeeld van hoe om die koste te bereken
# LLM-aanroep kost R 0,10 per aanroep
# Lookups in D1 kost R 0,05 per lookup
# Gemiddelde koste per gesprek = (10-15 beurte) x (1-2 LLM-aanroep + lookups in D1 per beurt) x koste per aanroep/lookup
Equipe OpenClaw
Kushicilelwe ngo May 30, 2026