● Architecture

Inside one turn: a workflow, not an agent loop

The steps are known in advance and the cost of a wrong step is a double booking, so code decides the next step. The model is used only to read words. Every turn returns the list of tools it called — the transcript is also the audit trail.

One turn, step by step

Every inbound WhatsApp message follows the same ten steps. Pick a message and watch it travel. Red-marked steps can stop the flow.

The booking transaction, line by line

The most important code in the system: capacity.commit(). Click a line — or let it step through.

The language layer and the LLM boundary

Rules read English, Hinglish, Hindi, Gujarati, Marathi, Punjabi and Marwari with no model call: clock times (11:30, 7 baje, 7 बजे), durations (dedh ghanta, डेढ़ घंटा), choices ("pehla"), safety words and urgency claims. A model is called only when rules return UNKNOWN.

Checks in code after the model replies

TIMESEvery time must lie between as-of − 1 h and as-of + 24 h.
EVIDENCEEvery evidence string must appear verbatim in the driver's message.
SAFETYChecked against the word list as well — either source escalates.
OUTPUT GUARDEvery dock code and clock time in an outgoing reply must appear in this turn's evidence. "Booked" may appear only after a commit returned CONFIRMED. In development it blocked one of our own replies — and was right.

Strict JSON shape for the fallback

{
  "intent": ["REPORT_DELAY","EARLY_ARRIVAL","ASK_OPTIONS",
             "CHECK_STATUS","CHOOSE","DECLINE",
             "ASK_HUMAN","SAFETY","UNKNOWN"],
  "eta": { "kind": ["CLOCK","DELAY","WINDOW","VAGUE"],
           "clock": "^([01][0-9]|2[0-3]):[0-5][0-9]$",
           "delay_min": "0..720",
           "window": ["HH:MM","HH:MM"],
           "hedged": true },
  "limits": { "earliest_start": null, "latest_gate_out": null },
  "shipment_hint": ["MORNING","EVENING","HEAVY","REEFER",null],
  "choice": "1..3",
  "safety": false,
  "evidence": { "": "" }
}
// additionalProperties: false — anything else is UNKNOWN
// The model's output can change what we ask, never what we book.

From prototype to pilot

The decision engine runs in the demo. The production layer around it is now in the repository, with 64 offline tests (40 of them for the WhatsApp channel). The driver channel runs on WhatsApp through Twilio: it has been played end to end through a local simulator that sends Twilio-signed webhooks and delivery receipts, but a round trip on the live Twilio Sandbox has not been run yet (steps in docs/twilio_live_test.md). It has also not yet run against Cognito, a real Redis server, LangSmith or the Anthropic API.

LayerComponent · technologyFile in the repoDemo engine
InboundFastAPI; WhatsApp Business Cloud API webhook, signature verifiedapp.pyworkflow.handle(phone, text, as_of, external_id)
IdentityDriver = WhatsApp number; coordinator = Cognito sign-in (email or mobile)setuhaul/auth.pyPhone looked up in drivers
SessionsRedis: sessions, tokens, history, dedupe, per-driver lock, rate limitsetuhaul/session_store.pyconversations + inbound_messages tables
OrchestrationLangGraph StateGraph: verify → dedupe → read → safety → dispatchsetuhaul/graph.pySession.run()
WordsRules; Claude Haiku via LangChain only when rules say UNKNOWN, strict JSON checked in codesetuhaul/nlu.py · setuhaul/llm.pyRules; a clarifying question instead of the model
ToolsETA, feasibility, planner, booking, escalation — plain Pythoneta · timeline · planner · capacity · escalationSame
System of recordMVP 1: SQLite on the server (one writer, overlap trigger, nightly backups). MVP 2: Postgres (EXCLUDE constraint) or MongoDBsetuhaul/db.pySQLite, BEGIN IMMEDIATE and triggers
OutboundOutbox drained to WhatsApp, SMS, e-mail, phone taskapp.py send_whatsapp()Outbox table
ObservabilityLangSmith trace per turn, phones masked, guardrail tagssetuhaul/tracing.pyaudit_log table
HumansCoordinator console: approve, reject, hand-offs, boardapp.py /console/*This prototype's Hand-offs page
CostOne small server (app + SQLite + Redis) ≈ ₹3,000 a month for MVP 1; MVP 2 adds a managed Postgres or MongoDB (about ₹5,000 a month) (see the walkthrough script's cost tables)