FREE COURSE · MODULE 4 · LESSON 4.2 🚪HANDS-ON CODE
WhatsApp AI Agent Mastery

The Language Detector at the Gate

Pillar 1 becomes live code today. Build the detector that runs before every other layer — looks at the incoming message, decides which of 5 language pipelines should handle it, routes the request. Average latency under 50ms. Under 20¢/day per 1,000 messages.

<50msAvg latency
< $0.20Per 1k msgs/day
5Language routes

The 5 Steps You'll Execute

Every WhatsApp message hitting your webhook is classified in under 50ms and routed to the matching language pipeline.

1

Claude Classifier

6 lines of Python. System prompt: 'classify text into fr, en, sw, ar, rn. Return JSON only.' anthropic.messages.create() → {'lang': 'fr', 'confidence': 0.97}. ~300ms latency, ~$0.001/call. Accurate on long + code-switched messages.

2

fastText Fallback

Facebook's pretrained lid.176.bin (130 MB, free). Load once at server boot. model.predict(text) → ('__label__fr', 0.94). Sub-5ms latency, zero cost, zero network call. Runs offline. Accuracy drops on very short + heavily code-switched text.

3

Confidence Threshold

Hybrid logic. Run fastText first (free + fast). If confidence > 0.85 → trust it, route immediately. If confidence < 0.85 → ambiguous, escalate to Claude. ~80% of messages clear fastText directly; only 20% pay the Claude latency.

4

Route the Detected Language

Simple switch statement → 5 language-specific pipelines (built in Lesson 4.3). 'fr' → French prompt + French KB shard. 'sw' → Swahili pipeline. And so on. The detector is the traffic cop; the pipelines do the work.

5

Edge Cases

Code-switching: pick dominant, add 'also speaks X' note to prompt. Ultra-short ('hi', 'merci'): default to customer profile language, fallback English. Profile override: customer-set language ALWAYS beats auto-detect. Log every detection decision.

Cost & latency — detection layer
< $0.20/day · <50ms
fastText (80% of traffic): free, local, 5ms · Claude classifier (20% escalation): $0.001/call, 300ms · Customer never notices.

Want a multilingual AI agent shipped for your business?

Top AI Africa deploys the full stack — language detection, multilingual prompts, native KB per language. WhatsApp agents that sound native in French, English, Swahili, Arabic, and Kirundi. Free 15-min strategy call.