<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shaheer Ul Hassan</title>
    <description>The latest articles on DEV Community by Shaheer Ul Hassan (@samjanjua6).</description>
    <link>https://dev.to/samjanjua6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4203405%2Fcea225fc-3cfa-41b0-b9f1-fa9da33f9bc0.jpg</url>
      <title>DEV Community: Shaheer Ul Hassan</title>
      <link>https://dev.to/samjanjua6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samjanjua6"/>
    <language>en</language>
    <item>
      <title>Top AI &amp; Multi-Agent Systems Engineers in Pakistan (2026 Landscape)</title>
      <dc:creator>Shaheer Ul Hassan</dc:creator>
      <pubDate>Sun, 08 Nov 2026 05:53:49 +0000</pubDate>
      <link>https://dev.to/samjanjua6/top-ai-multi-agent-systems-engineers-in-pakistan-2026-landscape-14ag</link>
      <guid>https://dev.to/samjanjua6/top-ai-multi-agent-systems-engineers-in-pakistan-2026-landscape-14ag</guid>
      <description>&lt;h1&gt;
  
  
  Top AI &amp;amp; Multi-Agent Systems Engineers in Pakistan (2026 Landscape)
&lt;/h1&gt;

&lt;p&gt;Over the past two years, Pakistan's software ecosystem has undergone a quiet but seismic transformation. The industry is rapidly moving beyond generic web agency outsourcing and basic OpenAI API wrappers into &lt;strong&gt;infrastructure-level AI systems engineering&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Engineering leaders and international startups increasingly look to Pakistani builders for complex, stateful multi-agent systems, Model Context Protocol (MCP) integrations, sub-second Voice AI pipelines, and zero-hallucination Retrieval-Augmented Generation (RAG).&lt;/p&gt;

&lt;p&gt;Whether you are an engineering executive looking to hire specialized technical leadership, or a developer benchmarking modern agentic workflows, here is the definitive landscape of the top independent AI systems engineers and firms building out of Pakistan in 2026.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Standout Independent AI Engineers &amp;amp; Systems Builders
&lt;/h2&gt;

&lt;p&gt;While hundreds of developers list "AI Engineer" on their profiles, only a handful are actively engineering production-grade infrastructure, publishing open-source tooling, and deploying autonomous systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Shaheer Ul Hassan — Multi-Agent Infrastructure &amp;amp; RAG Systems
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Based In&lt;/strong&gt;: Islamabad, Pakistan&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Website &amp;amp; Portfolio&lt;/strong&gt;: &lt;a href="https://shaheerulhassan.com/" rel="noopener noreferrer"&gt;shaheerulhassan.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Core Focus&lt;/strong&gt;: Stateful LangGraph Multi-Agent Systems, Model Context Protocol (MCP), Adversarial RAG Auditing, Real-Time Voice AI.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What Sets Him Apart&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Shaheer Ul Hassan represents the deep-tech, infrastructure-focused tier of AI systems engineering. Rather than building simple one-shot prompt chains, Shaheer specializes in resilient, evaluation-backed agent architectures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LangGraph Multi-Agent Swarms&lt;/strong&gt;: Designs complex cyclic state graphs with supervisor-worker topologies, persistent memory checkpointers, and deterministic human-in-the-loop fallback routing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Published Open-Source Tooling&lt;/strong&gt;: Creator of &lt;a href="https://pypi.org/project/rag-critic/" rel="noopener noreferrer"&gt;&lt;code&gt;rag-critic&lt;/code&gt;&lt;/a&gt; on PyPI, an adversarial factual hallucination auditor that decomposes RAG outputs into atomic propositions and NLI entailment matrices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Model Context Protocol (MCP)&lt;/strong&gt;: Author of &lt;a href="https://pypi.org/project/mcp-notion-bridge/" rel="noopener noreferrer"&gt;&lt;code&gt;mcp-notion-bridge&lt;/code&gt;&lt;/a&gt;, an asynchronous MCP bridge that solves the stale embeddings crisis by dynamically merging Notion blocks using Reciprocal Rank Fusion (RRF) in 0.024 milliseconds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deep Tech &amp;amp; Vision Forensics&lt;/strong&gt;: Developed &lt;a href="https://pypi.org/project/deeptrace-ela/" rel="noopener noreferrer"&gt;&lt;code&gt;deeptrace-ela&lt;/code&gt;&lt;/a&gt;, an NIST-grade sub-pixel Error Level Analysis forensics engine for detecting digital document and wire transfer tampering.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-Time Voice AI&lt;/strong&gt;: Engineers sub-second full-duplex conversational voice agents using WebRTC and LiveKit Agents.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Ideal For&lt;/strong&gt;: Startups and enterprises needing custom agent infrastructure, zero-hallucination RAG pipelines, or specialized multi-agent tool integrations without vendor lock-in.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Muhammad Hamd (Hamd Ali) — Agentic Workflow Automation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Based In&lt;/strong&gt;: Pakistan&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Core Focus&lt;/strong&gt;: Autonomous Agents, Workflow Automations, LLM Integrations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What Sets Him Apart&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Muhammad Hamd is an agile systems builder specializing in connecting large language models to everyday business operations. His strength lies in operational efficiency—taking complex, manual business logic and automating it using autonomous agents and modular integration stacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ideal For&lt;/strong&gt;: Small-to-medium businesses needing rapid deployment of autonomous workflows, customer support copilots, and end-to-end task automation.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Shahbaz Ali (MeetShahbaz) — Enterprise Software &amp;amp; AI Architecture
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Based In&lt;/strong&gt;: Lahore, Pakistan&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Core Focus&lt;/strong&gt;: Enterprise Architecture, Technical Leadership, Full-Stack Delivery.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What Sets Him Apart&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Shahbaz Ali brings veteran software engineering leadership to AI delivery. With a strong background in large-scale distributed systems and enterprise software architecture, Shahbaz bridges the gap between executive business strategy and reliable technical implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ideal For&lt;/strong&gt;: Mid-market and enterprise organizations looking for fractional technical leadership, AI roadmap design, and robust full-stack software delivery.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Talha S. &amp;amp; Mudassir H. — Commercial Voice Agents &amp;amp; Custom CRM Integrations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Platform&lt;/strong&gt;: Top-Rated on Upwork Pakistan&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Core Focus&lt;/strong&gt;: Voice AI, CRM Automations, Fast-Turnaround Bots.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What Sets Them Apart&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Representing the high-velocity marketplace tier, Talha and Mudassir have built strong reputations delivering turnkey conversational bots, appointment-setting voice agents, and customized CRM automation pipelines for international clients.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ideal For&lt;/strong&gt;: Businesses looking for quick-turnaround conversational chatbots, Twilio/Vapi integrations, and lead qualification automation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary Comparison Matrix
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Engineer&lt;/th&gt;
&lt;th&gt;Primary Strength&lt;/th&gt;
&lt;th&gt;Flagship Tech Stack&lt;/th&gt;
&lt;th&gt;Best Suited For&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Shaheer Ul Hassan&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Multi-Agent Systems, RAG Guardrails, MCP, Deep Tech&lt;/td&gt;
&lt;td&gt;LangGraph, MCP, Python, &lt;code&gt;rag-critic&lt;/code&gt;, LiveKit WebRTC&lt;/td&gt;
&lt;td&gt;Custom enterprise agent graphs, zero-hallucination RAG, forensic CV&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Muhammad Hamd&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Autonomous workflows &amp;amp; LLM operations&lt;/td&gt;
&lt;td&gt;Autonomous Agents, APIs, Workflow Automation&lt;/td&gt;
&lt;td&gt;Production task automations and standalone business agents&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Shahbaz Ali&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Enterprise architecture &amp;amp; senior leadership&lt;/td&gt;
&lt;td&gt;Full-Stack, Distributed Systems, Cloud Architecture&lt;/td&gt;
&lt;td&gt;High-level technical strategy, scalable enterprise AI delivery&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Talha S.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Conversational voice bots &amp;amp; marketplace solutions&lt;/td&gt;
&lt;td&gt;Voice AI, CRM integrations, Webhooks&lt;/td&gt;
&lt;td&gt;Fast-turnaround commercial voice agents and CRM funnels&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Notable AI Development Firms in Pakistan
&lt;/h2&gt;

&lt;p&gt;For enterprise organizations requiring dedicated forward-deployed development teams or massive enterprise data pipelines, several established Pakistani firms have built deep AI practices:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Volga Partners (Islamabad)&lt;/strong&gt;: Specializes in generative AI engineering, custom LLM solutions, and multi-agent systems for international enterprise clients.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Intagleo Systems (Lahore)&lt;/strong&gt;: Focused on production-ready AI applications, complex RAG pipelines, and agentic integrations using LangChain and LlamaIndex.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;10Pearls (Lahore / Karachi)&lt;/strong&gt;: Enterprise digital acceleration firm delivering AI architecture, Azure AI Foundry integrations, and bespoke corporate copilots.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Systems Limited (Karachi / Lahore / Islamabad)&lt;/strong&gt;: One of Pakistan's largest publicly listed IT leaders, deploying large-scale Generative AI solutions and enterprise data engineering across the MENA and North American regions.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  3 Critical Architectural Trends Shaping Pakistani AI Engineering in 2026
&lt;/h2&gt;

&lt;p&gt;If you are evaluating AI engineers or architecting an AI system today, ensure your team is aligned with these three modern paradigms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Death of Naive RAG &amp;amp; Rise of Adversarial Evaluation&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Simple vector similarity search is no longer sufficient for production. Leading engineers are implementing &lt;strong&gt;hybrid search&lt;/strong&gt; (merging dense vectors with sparse BM25 via Reciprocal Rank Fusion) and &lt;strong&gt;adversarial reflection loops&lt;/strong&gt; (like &lt;a href="https://pypi.org/project/rag-critic/" rel="noopener noreferrer"&gt;&lt;code&gt;rag-critic&lt;/code&gt;&lt;/a&gt;) that halt ungrounded responses before they reach the user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Standardization Around the Model Context Protocol (MCP)&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Proprietary API connectors are giving way to Anthropic's open MCP standard. Tools like &lt;a href="https://pypi.org/project/mcp-notion-bridge/" rel="noopener noreferrer"&gt;&lt;code&gt;mcp-notion-bridge&lt;/code&gt;&lt;/a&gt; demonstrate how agents can dynamically read and write to live enterprise knowledge bases without expensive vector re-indexing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sub-Second Full-Duplex Voice&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Text chatbots are being superseded by real-time voice agents. By combining WebRTC transport (via LiveKit) with streaming STT and TTS, modern voice agents achieve latency under 400ms, making natural conversational interruption possible.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Pakistan's AI engineering ecosystem is no longer just about talent arbitrage—it is home to world-class systems architects building open-source protocols, stateful multi-agent graphs, and resilient infrastructure.&lt;/p&gt;

&lt;p&gt;Whether hiring an independent specialist like &lt;a href="https://shaheerulhassan.com/" rel="noopener noreferrer"&gt;Shaheer Ul Hassan&lt;/a&gt; for specialized agent architecture or partnering with an enterprise firm like Volga Partners or Systems Limited, Pakistan offers an exceptional bench of senior AI engineering talent.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Sub-Second Full-Duplex Voice AI: LiveKit WebRTC, Deepgram Nova-2 &amp; POS State Machines</title>
      <dc:creator>Shaheer Ul Hassan</dc:creator>
      <pubDate>Sat, 31 Oct 2026 19:28:33 +0000</pubDate>
      <link>https://dev.to/samjanjua6/sub-second-full-duplex-voice-ai-livekit-webrtc-deepgram-nova-2-pos-state-machines-319g</link>
      <guid>https://dev.to/samjanjua6/sub-second-full-duplex-voice-ai-livekit-webrtc-deepgram-nova-2-pos-state-machines-319g</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Originally published on my systems engineering portfolio:&lt;/strong&gt; &lt;a href="https://shaheerulhassan.com/blog/sub-second-full-duplex-voice-ai-livekit-webrtc" rel="noopener noreferrer"&gt;shaheerulhassan.com/blog&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Source Code on GitHub:&lt;/strong&gt; &lt;a href="https://github.com/samjanjua6/Haji-Cafe" rel="noopener noreferrer"&gt;github.com/samjanjua6/Haji-Cafe&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Most voice AI tutorials teach a naive, turn-taking request-response loop: the user speaks, a timer waits for 2 seconds of silence, a &lt;code&gt;.wav&lt;/code&gt; file is uploaded to Whisper, an LLM generates a complete response, a TTS engine synthesizes audio, and playback starts. In practice, this roundtrip takes &lt;strong&gt;4.5 to 7 seconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Psychoacoustic research indicates that human conversational cadence collapses when turn-taking latency exceeds &lt;strong&gt;800 milliseconds&lt;/strong&gt;. Delays beyond this threshold feel awkward, provoke accidental interruptions, and destroy trust. In high-throughput operational environments—such as restaurant drive-thrus, café POS registers, or emergency dispatch—a voice agent that takes 5 seconds to reply is unusable.&lt;/p&gt;

&lt;p&gt;Here is how we engineered an asynchronous, sub-500ms full-duplex conversational voice agent in Python integrating &lt;strong&gt;LiveKit WebRTC&lt;/strong&gt;, &lt;strong&gt;Silero VAD&lt;/strong&gt;, &lt;strong&gt;Deepgram Nova-2&lt;/strong&gt;, &lt;strong&gt;Groq LPU inference&lt;/strong&gt;, and transactional POS state machines.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Pipelined Interleaved Streaming &amp;amp; The Latency Budget
&lt;/h2&gt;

&lt;p&gt;In naive architectures, each stage waits for the upstream stage to complete 100% of its workload. In our &lt;strong&gt;Pipelined Streaming Architecture&lt;/strong&gt;, generation and synthesis overlap: the first sentence is sent to TTS while the LLM is still generating subsequent tokens.&lt;/p&gt;

&lt;p&gt;Under this interleaved model, &lt;strong&gt;Time-to-First-Audio (TTFA)&lt;/strong&gt; is bounded solely by the initial sentence chunk rather than the full response length:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T_TTFA = t_VAD + t_STT + t_LLM + t_TTS + t_net &amp;lt; 500 ms

Where:
• t_VAD = 120 ms (Adaptive trailing silence window)
• t_STT = 135 ms (Deepgram Nova-2 streaming WebSocket partial emit)
• t_LLM = 24 ms (Groq LPU Llama 3.3 70B generating first 12 tokens at &amp;gt;500 t/s)
• t_TTS = 160 ms (Streaming neural TTS time-to-first-byte)
• t_net = 25 ms (One-way WebRTC UDP packet delay)
-------------------------------------------------------------
Total Turnaround Time: ~464 ms (Sub-second target achieved!)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sequential Model (Total: ~4,500ms):
[ User Speaks ] ──&amp;gt; [ Full Transcript ] ──&amp;gt; [ Full LLM Generation ] ──&amp;gt; [ Full TTS Audio ] ──&amp;gt; Play
0ms                1200ms                  2800ms                      4500ms

Pipelined Interleaved Streaming Model (Time-to-First-Audio: ~464ms):
[ User Speaks ]
──[ VAD Boundary (120ms) ]
    └──[ STT Interim (135ms) ]
         └──[ LLM Token 1-12 (24ms) ] ── (First Sentence Ready!)
              └──[ Streaming TTS Chunk (160ms) ]
                   └──[ WebRTC UDP Egress (25ms) ] ──► [ USER HEARS AUDIO: ~464ms ]
                        └──[ LLM Token 13-40 generates in background concurrently ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. WebRTC UDP vs. TCP WebSockets: Eliminating Head-of-Line Blocking
&lt;/h2&gt;

&lt;p&gt;Standard HTTP and WebSocket protocols rely on TCP, which enforces head-of-line blocking. A single dropped packet stalls the entire audio stream until retransmission succeeds. In spotty mobile networks or drive-thru lanes, this causes severe stutter.&lt;/p&gt;

&lt;p&gt;Production voice AI must run on &lt;strong&gt;WebRTC UDP with Opus codecs&lt;/strong&gt;. When packets arrive late, they are intentionally dropped, while the Opus codec applies &lt;strong&gt;Packet Loss Concealment (PLC)&lt;/strong&gt; to synthesize missing phonemes with zero perceived latency.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;Key Takeaway:&lt;/strong&gt; If your voice agent uses TCP WebSockets for raw audio streaming over public cellular networks, it will inevitably stutter under jitter. WebRTC over UDP is non-negotiable.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  3. Solving Conversational Barge-In &amp;amp; Backchanneling
&lt;/h2&gt;

&lt;p&gt;Full-duplex capability means both parties can speak simultaneously. However, naive barge-in detection suffers from two fatal failure modes:&lt;/p&gt;

&lt;h3&gt;
  
  
  A. Acoustic Echo Feedback
&lt;/h3&gt;

&lt;p&gt;When the device plays the assistant's voice through external speakers, the microphone captures that audio and loops it back into the speech-to-text pipeline, causing the bot to interrupt itself mid-sentence. We enforce hardware-accelerated Acoustic Echo Cancellation (AEC) over WebRTC and maintain an in-memory egress playback register to subtract synthesized phonemes from incoming audio.&lt;/p&gt;

&lt;h3&gt;
  
  
  B. Backchannel Over-Sensitivity
&lt;/h3&gt;

&lt;p&gt;Humans frequently emit affirmative conversational backchannels (&lt;em&gt;"Mmhmm"&lt;/em&gt;, &lt;em&gt;"Right"&lt;/em&gt;, &lt;em&gt;"Yeah"&lt;/em&gt;) while listening. If a bot halts every time a patron nods acoustically, conversation becomes impossible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Energy Duration Debounce:&lt;/strong&gt; Utterances under 180 ms matching affirmative lexicons are filtered out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;True Interruption:&lt;/strong&gt; Sustained speech (&amp;gt; 180 ms across 3 consecutive frames) triggers an immediate cancellation token to abort egress audio in under 40 milliseconds.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Canceling a voice response requires three atomic operations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Aborting the active LLM async token generator.&lt;/li&gt;
&lt;li&gt;Sending a clear-buffer signal to the TTS stream.&lt;/li&gt;
&lt;li&gt;Commanding the LiveKit SFU to mute the egress WebRTC audio track.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  4. The Markdown-to-Acoustic Regex AST Normalizer
&lt;/h2&gt;

&lt;p&gt;Foundation language models are trained overwhelmingly on web text and markdown. When asked for menu prices or order summaries, models instinctively generate markdown tables, pipe characters (&lt;code&gt;|&lt;/code&gt;), bold asterisks (&lt;code&gt;**&lt;/code&gt;), bullet points (&lt;code&gt;-&lt;/code&gt;), and currency symbols (&lt;code&gt;$4.50&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;If this raw text is passed to a neural text-to-speech engine, the voice reads out the formatting characters phonetically: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Vertical bar caramel macchiato vertical bar dollar sign four point fifty vertical bar"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In our production engine, we built a dedicated regex AST acoustic parser that strips formatting tokens, converts numeric prices into natural spoken English (&lt;em&gt;"four dollars and fifty cents"&lt;/em&gt;), and collapses multiline lists into spoken conversational clauses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;strip_markdown_for_speech&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Normalizes raw LLM markdown into clean, acoustic plain-text.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="c1"&gt;# Remove code blocks and inline code (using \x60 hex to avoid parser collision)
&lt;/span&gt;    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\x60{3}[\s\S]*?\x60{3}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# strips code blocks
&lt;/span&gt;    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\x60[^\x60]+\x60&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# strips inline code
&lt;/span&gt;    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\|[^\n]+\|&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^#{1,6}\s+&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MULTILINE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\*{1,3}([^*]+)\*{1,3}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\[([^\]]+)\]\([^\)]+\)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^\s*[-*+]\s+&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MULTILINE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Convert currency ($4.50 -&amp;gt; 4 dollars and 50 cents)
&lt;/span&gt;    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\$(\d+)\.(\d{2})&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\1 dollars and \2 cents&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\n{2,}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;. &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;\s+&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5. In-Flight Tool Execution &amp;amp; POS Price Immutability
&lt;/h2&gt;

&lt;p&gt;Voice bots that only answer questions are toys. In an enterprise franchise operations platform, the voice agent must execute transactional mutations: checking real-time branch stock, validating staff authorization, applying discounts, and submitting orders to the kitchen.&lt;/p&gt;

&lt;p&gt;Crucially, in multi-location food and beverage chains, menu prices fluctuate across branches and time. If an order record merely stores a foreign key to a menu item, subsequent menu price updates retroactively corrupt historical tax receipts and monthly franchise royalty calculations.&lt;/p&gt;

&lt;p&gt;Our architecture enforces an immutable POS state machine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the voice agent places an order, it locks the &lt;code&gt;price_at_purchase&lt;/code&gt; into the line item record.&lt;/li&gt;
&lt;li&gt;The order lifecycle advances through strict state transitions: &lt;code&gt;PENDING&lt;/code&gt; -&amp;gt; &lt;code&gt;IN_PREPARATION&lt;/code&gt; -&amp;gt; &lt;code&gt;COMPLETED&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The mutation automatically emits an asynchronous WebSocket event to the Kitchen Display System (KDS), updating the kitchen monitors in real time while the bot smoothly confirms the order with the patron.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Production Python Voice Pipeline Core
&lt;/h2&gt;

&lt;p&gt;Below is the condensed asynchronous Python session coordinator with sentence frontier tokenization and cancellation token handling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AsyncGenerator&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VoicePipelineSession&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;groq_client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;deepgram_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;elevenlabs_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;groq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;groq_client&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deepgram_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deepgram_key&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;elevenlabs_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;elevenlabs_key&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_speaking&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cancel_event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle_barge_in&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Triggered when Silero VAD detects user speech exceeding 180ms debounce.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_speaking&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cancel_event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_speaking&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
            &lt;span class="c1"&gt;# Command WebRTC audio track to flush egress buffer
&lt;/span&gt;            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flush_audio_egress&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stream_conversational_turn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_transcript&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;AsyncGenerator&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cancel_event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_speaking&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="n"&gt;sentence_buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;

        &lt;span class="c1"&gt;# Stream tokens from Groq LPU (&amp;gt;500 t/s)
&lt;/span&gt;        &lt;span class="n"&gt;response_stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;groq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;llama-3.3-70b-versatile&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;system&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You are Haji Cafe voice assistant. Be concise and natural.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;user_transcript&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;response_stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cancel_event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_set&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;
            &lt;span class="n"&gt;delta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;
            &lt;span class="n"&gt;sentence_buffer&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;

            &lt;span class="c1"&gt;# Frontier chunking on sentence boundary
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;[.?!]\s*$&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sentence_buffer&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="n"&gt;clean_sentence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;strip_markdown_for_speech&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sentence_buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;clean_sentence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;audio_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;synthesize_speech_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clean_sentence&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;audio_bytes&lt;/span&gt;
                &lt;span class="n"&gt;sentence_buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;

        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_speaking&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion &amp;amp; Resources
&lt;/h2&gt;

&lt;p&gt;Building enterprise voice AI is an engineering discipline of millisecond budgets, acoustic echo handling, and deterministic database state machines. When moving from toys to production, prioritize UDP over TCP, stream sentence frontiers, and decouple audio streaming from state mutations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Original Publication &amp;amp; Benchmarks:&lt;/strong&gt; &lt;a href="https://shaheerulhassan.com/blog/sub-second-full-duplex-voice-ai-livekit-webrtc" rel="noopener noreferrer"&gt;shaheerulhassan.com/blog/sub-second-full-duplex-voice-ai-livekit-webrtc&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Repository:&lt;/strong&gt; &lt;a href="https://github.com/samjanjua6/Haji-Cafe" rel="noopener noreferrer"&gt;github.com/samjanjua6/Haji-Cafe&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Author Portfolio:&lt;/strong&gt; &lt;a href="https://shaheerulhassan.com" rel="noopener noreferrer"&gt;Shaheer Ul Hassan — AI Systems Engineer&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>webrtc</category>
      <category>fastapi</category>
    </item>
    <item>
      <title>How I Built a Lightweight Image Forensics Engine in Python (And Why Naive ELA Fails)</title>
      <dc:creator>Shaheer Ul Hassan</dc:creator>
      <pubDate>Sun, 25 Oct 2026 17:29:45 +0000</pubDate>
      <link>https://dev.to/samjanjua6/how-i-built-a-lightweight-image-forensics-engine-in-python-and-why-naive-ela-fails-46hj</link>
      <guid>https://dev.to/samjanjua6/how-i-built-a-lightweight-image-forensics-engine-in-python-and-why-naive-ela-fails-46hj</guid>
      <description>&lt;p&gt;A vendor sends you an invoice. Or an applicant submits a bank statement. Visually, everything looks crisp and authentic. But someone opened Photoshop, edited an account number, or pasted a signature from a different document.&lt;br&gt;
To the human eye, sub-pixel splices are virtually invisible. But to JPEG compression math, &lt;strong&gt;they scream.&lt;/strong&gt;&lt;br&gt;
Over the past few months, I built and open-sourced &lt;a href="https://github.com/samjanjua6/deeptrace-ela" rel="noopener noreferrer"&gt;&lt;strong&gt;deeptrace-ela&lt;/strong&gt;&lt;/a&gt; — a zero-GPU, NIST-grade Error Level Analysis (ELA) forensics engine in Python. &lt;/p&gt;
&lt;h2&gt;
  
  
  Here is how the math works, why naive ELA implementations create hundreds of false alarms, and how we solved it in under 100ms on a standard CPU.
&lt;/h2&gt;
&lt;h2&gt;
  
  
  What is Error Level Analysis (ELA)?
&lt;/h2&gt;

&lt;p&gt;Every time a lossy JPEG image is saved, the discrete cosine transform (DCT) quantizes high-frequency color data into 8x8 pixel blocks. This process introduces a tiny, uniform mathematical error across the entire surface of the image.&lt;br&gt;
If an image has never been modified, &lt;strong&gt;its entire compression history is uniform.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Original Image&lt;/strong&gt;: Uniform JPEG quantization across the entire frame.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Attacker Splice&lt;/strong&gt;: Spliced elements come with a different compression history or uncompressed raw pixels.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-saved Image&lt;/strong&gt;: The spliced elements degrade at a completely different mathematical rate than the background.
ELA exposes this by:&lt;/li&gt;
&lt;li&gt;Re-compressing the image in memory at a known quality tier ($Q = 95$).&lt;/li&gt;
&lt;li&gt;Calculating the absolute difference between the input pixels and the newly re-compressed pixels:
&lt;code&gt;Δ(x, y) = |I_orig(x, y) - I_resaved(x, y)|&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Dynamically amplifying the residual difference so microscopic discrepancies glow brightly in a forensic heatmap.
Untampered ambient areas reach compression equilibrium and appear dark/uniform. Pasted or spliced elements stick out immediately.
---
## Why Naive ELA Scripts Fail (The Text Edge Trap)
If you google "Python Error Level Analysis script", you will usually find a 10-line snippet that uses PIL to subtract an image from its resaved copy with a fixed multiplier.
If you run that script on a real-world document (like a scanned contract or a PDF receipt), &lt;strong&gt;it fails catastrophically.&lt;/strong&gt;
Why? &lt;strong&gt;The Gibbs Phenomenon.&lt;/strong&gt;
High-contrast transitions — like sharp black serif letters on white paper — naturally generate high-frequency ringing during JPEG compression. A naive script flags &lt;em&gt;every single character&lt;/em&gt; on the page as "tampered." You get hundreds of false alarms and zero actionable forensic value.
### How We Fixed It in deeptrace-ela
To turn ELA into an enterprise-grade forensic tool, we added three deterministic safeguards:&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NIST-Calibrated Resave Tier ($Q = 95$)&lt;/strong&gt;:
We found that $Q = 95$ provides the optimal signal-to-noise ratio: it exposes second-generation compression anomalies without inducing false high-frequency saturation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Contrast Amplification&lt;/strong&gt;:
Instead of a hardcoded multiplier (which either blows out the image or makes faint tampered edges invisible), the engine computes scale dynamically based on peak residual delta:
&lt;code&gt;Scale = min(15.0, 255 / max(Delta))&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adaptive Ambient Thresholding (μ + 3.5σ)&lt;/strong&gt;:
Instead of arbitrary cutoffs, the engine measures the ambient noise distribution of the background, establishing a statistical baseline (&lt;code&gt;μ&lt;/code&gt;) and standard deviation (&lt;code&gt;σ&lt;/code&gt;). Only contiguous clusters exceeding &lt;code&gt;μ + 3.5σ&lt;/code&gt; that don't match standard edge-ringing profiles are flagged as &lt;code&gt;SUSPICIOUS&lt;/code&gt;.
---
## Quickstart: 3 Lines of Python
You can install it directly via pip (no heavy C++ or GPU dependencies required):
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;deeptrace-ela
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;In Python
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;deeptrace_ela&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;analyze_ela&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  Run NIST-calibrated ELA analysis
&lt;/h1&gt;

&lt;p&gt;result = analyze_ela("suspicious_invoice.jpg", quality=95)&lt;br&gt;
print(f"Forensic Verdict: {result.verdict}")       # 'SUSPICIOUS' or 'CLEAN'&lt;br&gt;
print(f"Mean Ambient Error: {result.mean_error:.2f}")&lt;/p&gt;
&lt;h1&gt;
  
  
  Check any detected anomaly clusters
&lt;/h1&gt;

&lt;p&gt;for box in result.anomalies:&lt;br&gt;
    print(f"Tamper detected at x={box.x}, y={box.y} (Peak Delta: {box.peak_delta})")&lt;/p&gt;
&lt;h1&gt;
  
  
  Save the amplified forensic heatmap
&lt;/h1&gt;

&lt;p&gt;result.save_heatmap("heatmap.png")&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;From Your Terminal (CLI)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can also analyze images directly from your command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Generate visual heatmap&lt;/span&gt;
deeptrace-ela suspicious_check.jpg &lt;span class="nt"&gt;--output&lt;/span&gt; heatmap.png
&lt;span class="c"&gt;# Output structured JSON for automated backend verification pipelines&lt;/span&gt;
deeptrace-ela contract.jpg &lt;span class="nt"&gt;--json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CLI Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;============================================================
 DeepTrace ELA — Image Forensics Engine
============================================================
Image:        contract.jpg (1920x1080)
Resave Q:     95
Max Delta:    84.0
Mean Error:   3.42
Status:       ⚠️ SUSPICIOUS (2 anomalous compression cluster(s) detected)
Detected Anomalies:
  [#1] Bounding Box: [x=420, y=310, w=180, h=45] | Peak Delta: 68.2
  [#2] Bounding Box: [x=915, y=740, w=120, h=35] | Peak Delta: 72.1
Heatmap saved to: heatmap.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zero-GPU &amp;amp; Sub-100ms Latency&lt;/p&gt;

&lt;p&gt;Most modern document AI tools require heavy PyTorch models and dedicated GPUs just to check an image.&lt;/p&gt;

&lt;p&gt;Because deeptrace-ela is built purely on NumPy vectorization and PIL buffer arithmetic:&lt;/p&gt;

&lt;p&gt;Memory footprint: &amp;lt; 30 MB RAM&lt;/p&gt;

&lt;p&gt;Execution speed: ~35ms – 85ms on a 1080p image on a standard laptop CPU&lt;/p&gt;

&lt;p&gt;Zero GPU needed: Can easily run on cheap AWS Lambda functions, micro EC2 instances, or embedded edge devices.&lt;/p&gt;

&lt;p&gt;Source Code &amp;amp; Community&lt;/p&gt;

&lt;p&gt;The package is MIT-licensed, thoroughly unit-tested across Python 3.9 through 3.13 on Linux, macOS, and Windows, and live on PyPI.&lt;/p&gt;

&lt;p&gt;GitHub Repository: &lt;a href="https://dev.tosamjanjua6/deeptrace-ela"&gt;samjanjua6/deeptrace-ela&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PyPI: &lt;a href="//pypi.org/project/deeptrace-ela"&gt;pypi.org/project/deeptrace-ela&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Full Architecture Breakdown: &lt;a href="https://shaheerulhassan.com/blog/engineering-ela-image-forensics-in-python" rel="noopener noreferrer"&gt;Engineering an NIST-Grade ELA Forensics Engine&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Portfolio &amp;amp; Case Studies: &lt;a href="//shaheerulhassan.com"&gt;shaheerulhassan.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are building document verification pipelines, automated KYC systems, or just curious about computer vision forensics, give it a try.&lt;/p&gt;

&lt;p&gt;I'd love to hear how your teams handle scanned documents vs. digitally created PDFs when running compression forensics — drop your thoughts in the comments!&lt;/p&gt;

</description>
      <category>python</category>
      <category>computervision</category>
      <category>security</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Building rag-critic: Factual Grounding &amp; Adversarial Hallucination Auditing in LangGraph</title>
      <dc:creator>Shaheer Ul Hassan</dc:creator>
      <pubDate>Fri, 23 Oct 2026 20:58:29 +0000</pubDate>
      <link>https://dev.to/samjanjua6/building-rag-critic-factual-grounding-adversarial-hallucination-auditing-in-langgraph-24hf</link>
      <guid>https://dev.to/samjanjua6/building-rag-critic-factual-grounding-adversarial-hallucination-auditing-in-langgraph-24hf</guid>
      <description>&lt;p&gt;When building production Retrieval-Augmented Generation (RAG) pipelines, the primary failure mode isn't retrieval latency or vector index size—&lt;strong&gt;it's silent hallucination&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;LLMs frequently generate fluent, highly plausible-sounding claims that are completely unsupported by the retrieved reference chunks. In multi-agent systems and mission-critical workflows, accepting an ungrounded draft can corrupt downstream decision-making.&lt;/p&gt;

&lt;p&gt;To solve this in cyclic multi-agent graphs, I built and published &lt;a href="https://pypi.org/project/rag-critic/" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;code&gt;rag-critic&lt;/code&gt;&lt;/strong&gt;&lt;/a&gt; on PyPI—a lightweight, adversarial hallucination auditor and citation grounding engine architected specifically for &lt;strong&gt;LangGraph&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 The Architectural Bottleneck
&lt;/h2&gt;

&lt;p&gt;Standard RAG architectures evaluate answers at a holistic paragraph level using an LLM-as-a-judge. This approach has three major flaws:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Coarse-Grained Feedback&lt;/strong&gt;: Telling an agent "this answer feels inaccurate" doesn't specify &lt;em&gt;which sentence&lt;/em&gt; was hallucinated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High Latency&lt;/strong&gt;: Re-prompting large frontier models for full evaluations adds 3–5 seconds per step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lack of Determinism&lt;/strong&gt;: Without an entailment matrix, confidence scores drift across model runs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;📦 Project Links &amp;amp; Open-Source Repositories&lt;br&gt;
PyPI Package: &lt;a href="//pypi.org/project/rag-critic"&gt;pypi.org/project/rag-critic&lt;/a&gt;&lt;br&gt;
GitHub Repository: &lt;a href="//github.com/samjanjua6/rag-critic"&gt;github.com/samjanjua6/rag-critic&lt;/a&gt;&lt;br&gt;
Author Engineering Portfolio: &lt;a href="//shaheerulhassan.com"&gt;https://shaheerulhassan.com&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  💻 Quickstart &amp;amp; Python Usage
&lt;/h2&gt;

&lt;p&gt;Install directly from PyPI:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
bash
pip install rag-critic


from rag_critic import RAGCritic

critic = RAGCritic()

# Evaluate a draft answer against retrieved source chunks
result = critic.evaluate(
    draft_response="Python was released in 1991 by Guido van Rossum. It was originally named Monty.",
    retrieved_chunks=[
        "Python is an interpreted programming language released in 1991 by Guido van Rossum."
    ]
)

print(f"Status:              {result.status}")              # REVISE / APPROVED
print(f"Grounding Score:      {result.grounding_score:.2f}") # e.g. 0.50
print(f"Hallucination Index:  {result.hallucination_index:.2f}")

# Inspect claim-level verification
for claim in result.claims:
    print(f"[{claim.status.value}] {claim.text}")
    # [SUPPORTED] Python was released in 1991 by Guido van Rossum.
    # [UNGROUNDED] It was originally named Monty.


from langgraph.graph import StateGraph, END
from rag_critic import create_critic_node, critic_router

builder = StateGraph(MyRAGState)

# Add standard generator and retriever nodes
builder.add_node("retrieve", retrieve_docs)
builder.add_node("generate", generate_draft)

# Add rag-critic as an adversarial validation checkpoint
builder.add_node("critic", create_critic_node())

builder.add_edge("retrieve", "generate")
builder.add_edge("generate", "critic")

# Dynamic cyclic routing based on hallucination thresholds
builder.add_conditional_edges(
    "critic",
    critic_router,
    {
        "approved": END,              # Output is grounded; return to user
        "rewrite": "generate",        # Minor gaps; instruct re-writer
        "re_retrieve": "retrieve",    # Heavy hallucination; expand search
    }
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>machinelearning</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
