<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Bits & Bytes: AI,LLMs and latest tech]]></title><description><![CDATA[My thoughts and experiences as an AI engineer]]></description><link>https://thoughts.bitsnbytes.in/s/aillms-and-latest-tech</link><image><url>https://substackcdn.com/image/fetch/$s_!oZ9L!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf7f649e-2ae7-41dc-a4fc-f44298a46b4c_1024x1024.png</url><title>Bits &amp; Bytes: AI,LLMs and latest tech</title><link>https://thoughts.bitsnbytes.in/s/aillms-and-latest-tech</link></image><generator>Substack</generator><lastBuildDate>Sat, 11 Apr 2026 05:27:34 GMT</lastBuildDate><atom:link href="https://thoughts.bitsnbytes.in/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Aravind]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[aravindkondamudi@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[aravindkondamudi@substack.com]]></itunes:email><itunes:name><![CDATA[Aravind]]></itunes:name></itunes:owner><itunes:author><![CDATA[Aravind]]></itunes:author><googleplay:owner><![CDATA[aravindkondamudi@substack.com]]></googleplay:owner><googleplay:email><![CDATA[aravindkondamudi@substack.com]]></googleplay:email><googleplay:author><![CDATA[Aravind]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Building your first agentic system]]></title><description><![CDATA[The While Loop is the Easy Part: 5 Lessons from building Agentic systems]]></description><link>https://thoughts.bitsnbytes.in/p/building-your-first-agentic-system</link><guid isPermaLink="false">https://thoughts.bitsnbytes.in/p/building-your-first-agentic-system</guid><dc:creator><![CDATA[Aravind]]></dc:creator><pubDate>Tue, 06 Jan 2026 18:20:08 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!SLu1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SLu1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SLu1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!SLu1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!SLu1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!SLu1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SLu1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/db4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5727036,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://thoughts.bitsnbytes.in/i/183641336?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SLu1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!SLu1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!SLu1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!SLu1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4aaecc-9699-478e-bdc6-ee3712b36e5b_2816x1536.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thoughts.bitsnbytes.in/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thoughts.bitsnbytes.in/subscribe?"><span>Subscribe now</span></a></p><p><em>PS: Content and direction are mine, article heavily edited using AI.</em></p><p><br>2025 was called the year of agents. But until Claude Code shipped, truly agentic systems were sparse. I had built a multi-agent system earlier (<a href="https://thoughts.bitsnbytes.in/p/learnings-from-implementing-multi">read here</a>), but it was more orchestration than agentic.<br><br>Then it struck me: Claude Code, Codex &#8212; <em>they&#8217;re just while loops.</em><br><br>After exploring open-source tools and using Claude Code and Codex, I built a data analysis agent: upload a file, ask any question, and the agent figures out the columns it needs, runs the analysis, and returns a summary with charts. <a href="https://github.com/Aravgit/data-analyst.git">Code shared here</a>.<br><br>It worked great in development. Then it broke. I&#8217;ve built 4-5 versions next, all of which failed at various stages before reaching an optimal stage.<br><br>This post covers five <strong>reliability lessons</strong> that don&#8217;t show up in tutorials &#8212; but <em>will</em> show up in your debugging logs.</p><h3><strong>TL;DR: The Agent Builder Checklist</strong></h3><ul><li><p><strong>Memory isn&#8217;t free:</strong> Runtimes (GC or Ownership) won't save you from logical leaks in session state. Track your memory usage and explicitly clear intermediate resources after every turn.</p></li><li><p><strong>Prompt for efficiency:</strong> LLMs are greedy; they will load 50 columns when they need 1. Use proper context engineering.</p></li><li><p><strong>Compact between turns:</strong> Never truncate context mid-reasoning. Track token usage and prune &#8220;dead weight&#8221; before the next turn starts.</p></li><li><p><strong>Schemas are contracts:</strong> Vague tool descriptions lead to hallucinated parameters. Use strict, explicit schemas.</p></li><li><p><strong>Set circuit breakers:</strong> Agents loop until they fail or go broke. Implement hard limits on tool calls and retries.</p></li></ul><div><hr></div><h2>1) Memory That Never Dies</h2><p><strong>The assumption:</strong> Each agent turn is stateless. Resources clean up automatically.<br><strong>The reality:</strong> Most implementations keep session state between tool calls &#8212; intermediate results, loaded resources, computed values. That state accumulates.</p><pre><code>def execute_tool(self, tool_name, args):
    result = run_tool(tool_name, args)
    self.resources[args["name"]] = result  # accumulates forever
    return result</code></pre><p>Multiply that by 100 concurrent users running multi-step chains. Memory climbs. Garbage collection can&#8217;t help because references still exist. Eventually: OOM crashes.</p><p><strong>What happened:</strong> Data files loaded for analysis stayed in RAM after each turn. Python&#8217;s GC couldn&#8217;t reclaim them because the session held references. With concurrent users, memory grew unbounded until containers crashed.</p><p><strong>The fix:</strong> Explicit lifecycle management (clear references + close handles + enforce TTL/LRU).</p><pre><code><code>import gc

def reset_after_turn(session):
    # If your objects have a close()/cleanup() method, call it here.
    for key, obj in list(session.resources.items()):
        try:
            if hasattr(obj, "close"):
                obj.close()
        finally:
            session.resources.pop(key, None)
            del obj  # break references (especially helpful for complex graphs)

    session.resources.clear()
    gc.collect()  # GC is a backstop; real fix is removing session refs + closing handles
</code></code></pre><p><strong>Key insight:</strong> GC is a backstop, not a solution. If your session holds references, memory won't be freed. This is especially dangerous when using data libraries like <strong>Pandas or NumPy</strong>&#8212;they often allocate memory in C-extensions that Python&#8217;s GC struggles to track.Explicitly drop references + close resources, and enforce TTL/LRU for bounded session state.</p><div><hr></div><h2>2) The Agent Loads Everything</h2><p><strong>The assumption:</strong> The model will make efficient choices about resource loading.</p><p><strong>The reality:</strong> LLMs optimize for correctness, not efficiency. Given &#8220;load everything&#8221; vs &#8220;load only what&#8217;s needed,&#8221; they choose safety every time.</p><p><strong>What happened:</strong> I provided three loading tools &#8212; <code>sample</code> (schema only), <code>partial</code> (specific columns), and <code>full</code> (everything). The agent consistently chose full loads even for &#8220;what&#8217;s the average of column X?&#8221; Loading 50 columns when it needed 1.</p><p><strong>The fix:</strong> It wasn&#8217;t better tools &#8212; it was a better system prompt. Don&#8217;t just provide tools; tell the agent <strong>when</strong> to use each:</p><pre><code><code>LOADING STRATEGY (follow this order):
1) get_metadata()          - Check what's available FIRST
2) load_partial(fields)    - PRIMARY: Load ONLY what's needed
3) load_full()             - RARE: Only when ALL fields are required

Examples:
- "average sales by region" -&gt; load_partial(["region", "sales"])
- "filter by date"          -&gt; load_partial(["date", "amount"])
</code></code></pre><p>The pattern: provide a clear hierarchy with concrete examples. It&#8217;s still prompt engineering in 2026.</p><div><hr></div><h2>3) Context Overflow Mid-Turn</h2><p><strong>The assumption:</strong> Context windows are large enough.</p><p><strong>The reality:</strong> In practice, I found ~100k&#8211;250k tokens to be the usable range. The industry is moving away from large context windows, we now talk  doing context engineering. So when agent loops accumulate context fast: tool calls, tool results, intermediate reasoning &#8212; it adds up, we need to compact the context. Hit the limit mid-turn and the user gets nothing.</p><p>A typical turn:</p><ul><li><p>user message (100 tokens)</p></li><li><p>system prompt (1000)</p></li><li><p>tool call (300)</p></li><li><p>tool result (2000)</p></li><li><p>another call (100)</p></li><li><p>another result (1500)</p></li></ul><p>After a few turns, you&#8217;re at 80% capacity. One unexpectedly large tool result (e.g., analysis output on a 1000&#215;10 table) and you hit the wall mid-turn. The model can&#8217;t complete. The user&#8217;s question goes unanswered.</p><p><strong>What happened:</strong> Our compaction logic triggered mid-turn when tokens exceeded a threshold. This interrupted the agent&#8217;s reasoning. Users asked questions and got nothing back.</p><p><strong>The fix:</strong> Compact <strong>between</strong> turns, never during. Also be deliberate about <em>what</em> you compact: messages, tool outputs, raw data dumps &#8212; anything that won&#8217;t help the next turn. The goal here is <strong>Context Precision</strong>. By compacting between turns, you ensure the agent starts every new reasoning step with a "clean" but relevant history, rather than hitting a wall halfway through a critical computation.</p><pre><code><code>def run_agent_turn(session, message):
    # Check BEFORE the turn, not during
    if session.total_tokens &gt; (MAX_TOKENS * 0.80):
        compact_history(session)  # summarize / prune / store large artifacts out-of-band
    return execute_turn(session, message)
</code></code></pre><p>Don&#8217;t punish the user for token limits. Track usage and proactively compact before hitting the ceiling (unless your KPI is token usage, not outcomes, Claude Code - I am talking to you).</p><div><hr></div><h2>4) Tool Schemas Are Your API Contract</h2><p><strong>The assumption:</strong> Tool descriptions are just documentation for the model.</p><p><strong>The reality:</strong> Tool schemas are the contract between the model <em>and</em> your tool runner. The model uses them to decide what to call and how. Your runner uses them to validate and execute. Vague schemas = wrong calls = runtime errors on both sides. </p><pre><code><code>{
  "name": "process_data",
  "description": "Process the data",
  "parameters": {
    "data": { "type": "string" },
    "options": { "type": "object" }
  }
}
</code></code></pre><p>What data? What format? What options? The model guesses &#8212; and guesses wrong.</p><p><strong>The fix:</strong> Whether you&#8217;re using JSON Schema or a simplified tool schema format or a pydantic type-safe models, the point is: make it strict.Strict, explicit schemas:</p><pre><code><code>{
  "name": "load_dataset_columns",
  "description": "Load specific columns from a dataset. Returns a DataFrame.",
  "parameters": {
    "dataset_name": {
      "type": "string",
      "description": "Name from get_dataset_info()"
    },
    "columns": {
      "type": "array",
      "items": { "type": "string" },
      "description": "Column names to load"
    },
    "df_name": {
      "type": "string",
      "description": "Variable name for the DataFrame in REPL"
    }
  },
  "required": ["dataset_name", "columns", "df_name"]
}
</code></code></pre><p><strong>Key Insights</strong>:</p><ul><li><p>prefer explicit enums over free-form strings</p></li><li><p>describe relationships between tools (&#8220;use output from X&#8221;)</p></li><li><p>document return values (shape + keys)</p></li><li><p>mark required fields clearly</p></li></ul><p>The model can only be as precise as your schema allows.</p><div><hr></div><h2>5) Runaway Loops Will Burn Your Budget</h2><p><strong>The assumption:</strong> The agent will finish in a reasonable number of steps.</p><p><strong>The reality:</strong> Without limits, agents can loop indefinitely. A confused model retries the same failing tool. A complex query spawns endless sub-tasks. Each iteration costs tokens, time, and money.</p><p><strong>What happened:</strong> An edge case caused the agent to repeatedly call the same tool with slightly different parameters, expecting different results.</p><p><strong>The fix:</strong> Hard limits at multiple levels: max tool calls per turn, max retries per tool, and a timeout per turn.</p><pre><code><code>MAX_TOOL_CALLS_PER_TURN = 10
MAX_RETRIES_SAME_TOOL = 3

def run_agent_turn(session, message):
    tool_calls = 0
    tool_counts = {}

    while not done:
        response = model.generate(...)

        if response.has_tool_calls:
            for call in response.tool_calls:
                tool_counts[call.name] = tool_counts.get(call.name, 0) + 1
                if tool_counts[call.name] &gt; MAX_RETRIES_SAME_TOOL:
                    return {"error": "Tool retry limit exceeded"}

            tool_calls += len(response.tool_calls)
            if tool_calls &gt; MAX_TOOL_CALLS_PER_TURN:
                return {"error": "Tool call limit exceeded"}
</code></code></pre><p>When limits hit, fail gracefully with a clear message. The user can retry with a narrower question, or you can ask a clarifying question. Also add per-tool timeouts, exponential backoff, and a circuit breaker when the same error repeats.</p><div><hr></div><h2>Why Not Use Agentic Libraries?</h2><p>You might ask: why build from primitives instead of using LangChain, CrewAI, or similar frameworks?</p><p>Honestly, I chose not to. These libraries are still maturing, and the tools I admired &#8212; Claude Code, Codex &#8212; were built on raw foundations, not abstractions. I wanted to understand the while loop, not hide it.</p><p>That said, frameworks have their place. If you need to ship fast and your use case fits their patterns, use them. But if you&#8217;re debugging a memory leak at 2:30 AM (yes, I hit the Claude limits too &#128517;), you&#8217;ll want to know what&#8217;s actually happening under the hood.</p><div><hr></div><h2>Conclusion</h2><p>The biggest mistake is thinking an agent is just &#8220;an LLM in a while loop with tools.&#8221; It&#8217;s not. It&#8217;s a system with:</p><ul><li><p><strong>State management</strong> (sessions, resources, context)</p></li><li><p><strong>Resource constraints</strong> (memory, tokens, time)</p></li><li><p><strong>Failure modes</strong> (partial, silent, cascading)</p></li><li><p><strong>Concurrency concerns</strong> (multiple users, multiple turns)</p></li></ul><p>The while loop is the easy part. Everything around it &#8212; context management, resource efficiency, error handling, prompt engineering &#8212; that&#8217;s where the work lives.</p><p>Build agents like you&#8217;d build any production system: explicit resource management, defensive error handling, clear contracts, and guardrails everywhere.</p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thoughts.bitsnbytes.in/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thoughts.bitsnbytes.in/subscribe?"><span>Subscribe now</span></a></p><p>If you&#8217;re interested in the intersection of LLM orchestration and reliable AI engineering to solve analytics and other interesting problems, subscribe.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thoughts.bitsnbytes.in/p/building-your-first-agentic-system/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thoughts.bitsnbytes.in/p/building-your-first-agentic-system/comments"><span>Leave a comment</span></a></p><p><br></p>]]></content:encoded></item><item><title><![CDATA[Does "Thinking" Always Help an LLM?]]></title><description><![CDATA[High reasoning wins more often, especially on structured, instruction-heavy tasks, but it can stumble on exact code/SQL/schema work.]]></description><link>https://thoughts.bitsnbytes.in/p/does-thinking-help-an-llm</link><guid isPermaLink="false">https://thoughts.bitsnbytes.in/p/does-thinking-help-an-llm</guid><dc:creator><![CDATA[Aravind]]></dc:creator><pubDate>Mon, 01 Dec 2025 17:26:49 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!wgGu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wgGu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wgGu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!wgGu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!wgGu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!wgGu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wgGu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1654665,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://thoughts.bitsnbytes.in/i/180418977?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wgGu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!wgGu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!wgGu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!wgGu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c513fc-e8f7-4dc8-b056-eac915bf0990_1024x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p><strong>TL;DR</strong></p><ul><li><p>I ran ~100 hard, instruction-heavy tasks on the same base model (gpt-5.1) with three reasoning settings: none, low, and high. The high-reasoning variant had the highest average score and was picked best about half the time.</p></li><li><p>The biggest gains are on structured, multi-step, format-constrained tasks (analysis plans, comms, structured writing, accessible frontend). On precise code, SQL, and schema tasks, extra reasoning sometimes adds small syntax or logic errors.</p></li><li><p>Use high reasoning for complex, instruction-heavy prompts, and always pair it with tests or validation when the output must be executable (code, SQL, schemas).</p></li><li><p>All data, prompts, code are shared at the end of article</p></li></ul><h2>Why I Ran This</h2><p>I wanted to understand a simple question:</p><p><strong>If I only change the reasoning setting on a model, does quality actually improve, and where?</strong></p><p>Using the same base model (gpt-5.1), I varied only <code>reasoning.effort</code>:</p><ul><li><p><code>model_1</code>: reasoning none</p></li><li><p><code>model_2</code>: reasoning low</p></li><li><p><code>model_3</code>: reasoning high</p></li></ul><p>I then checked:</p><ol><li><p>Does higher reasoning effort improve instruction following and correctness?</p></li><li><p>Is the benefit uniform across task types, or concentrated in a few?</p></li></ol><p>This was partly inspired by the Apple paper &#8220;The Illusion of Thinking&#8221; and by watching ChatGPT &#8220;think&#8221; at length on prompts where it did not seem to help.</p><h2>Task Setup in Brief</h2><p>I asked gpt-5.1 with high reasoning to generate all tasks, with instructions to make them:</p><ul><li><p>Instruction heavy, multi step, and non trivial</p></li><li><p>Focused on realistic domains like SQL and analytics, Python jobs, frontend components, communications, docs and schemas, and planning or writing</p></li><li><p>Strict about format, for example &#8220;return only code blocks, JSON, or ordered sections&#8221;</p></li><li><p>Full of edge cases and explicit constraints, and to avoid simple or one line tasks</p></li></ul><p>I deduped by <code>(category, task_name, prompt)</code> and ended up with 98 tasks.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3bHT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3bHT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png 424w, https://substackcdn.com/image/fetch/$s_!3bHT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png 848w, https://substackcdn.com/image/fetch/$s_!3bHT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png 1272w, https://substackcdn.com/image/fetch/$s_!3bHT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3bHT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77111,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thoughts.bitsnbytes.in/i/180418977?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3bHT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png 424w, https://substackcdn.com/image/fetch/$s_!3bHT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png 848w, https://substackcdn.com/image/fetch/$s_!3bHT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png 1272w, https://substackcdn.com/image/fetch/$s_!3bHT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F059f71d8-8cc7-4b30-93e5-df0d2b9de429_2400x1600.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>How I Evaluated</h2><p>For each task:</p><ol><li><p><strong>Answer generation</strong><br>I called gpt-5.1 three times with the same prompt, once with each reasoning setting, and stored the outputs as <code>model_1</code>, <code>model_2</code>, and <code>model_3</code>.</p></li><li><p><strong>Scoring</strong><br>Another gpt-5.1 instance(could setup another LLM model here), acting as a strict grader, scored each answer from 0 to 10.<br>Scores were penalized for:</p><ul><li><p>Wrong format (for example, extra text when the prompt asked for &#8220;JSON only&#8221;)</p></li><li><p>Broken ordering or missing required sections</p></li><li><p>Clear logical or syntax errors</p></li></ul></li><li><p><strong>Best pick</strong><br>The grader then saw all three answers and had to pick <strong>one</strong> best model, no ties. This gives a &#8220;win rate&#8221; per model in addition to average scores.</p></li></ol><p>I also broke down results by broad category (sql, python, frontend, analysis, communication, schema, docs, planning, writing, and so on).</p><h2>Results</h2><p>Across all 98 tasks:</p><ul><li><p><strong>Average scores</strong></p><ul><li><p>model_1 (none): 7.52</p></li><li><p>model_2 (low): 8.19</p></li><li><p>model_3 (high): 9.09</p></li></ul></li><li><p><strong>Best model picks</strong></p><ul><li><p>model_1: 22.4% of tasks</p></li><li><p>model_2: 28.6% of tasks</p></li><li><p>model_3: 49.0% of tasks</p></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!G61i!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!G61i!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png 424w, https://substackcdn.com/image/fetch/$s_!G61i!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png 848w, https://substackcdn.com/image/fetch/$s_!G61i!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png 1272w, https://substackcdn.com/image/fetch/$s_!G61i!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!G61i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png" width="438" height="328.5" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1092,&quot;width&quot;:1456,&quot;resizeWidth&quot;:438,&quot;bytes&quot;:37286,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thoughts.bitsnbytes.in/i/180418977?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!G61i!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png 424w, https://substackcdn.com/image/fetch/$s_!G61i!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png 848w, https://substackcdn.com/image/fetch/$s_!G61i!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png 1272w, https://substackcdn.com/image/fetch/$s_!G61i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F624adc8e-11d0-4652-851f-80e10aabd197_1600x1200.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>model_3 wins most often, but the other two still win a meaningful fraction of tasks.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6tDJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6tDJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png 424w, https://substackcdn.com/image/fetch/$s_!6tDJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png 848w, https://substackcdn.com/image/fetch/$s_!6tDJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png 1272w, https://substackcdn.com/image/fetch/$s_!6tDJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6tDJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png" width="518" height="388.5" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1092,&quot;width&quot;:1456,&quot;resizeWidth&quot;:518,&quot;bytes&quot;:32557,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thoughts.bitsnbytes.in/i/180418977?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6tDJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png 424w, https://substackcdn.com/image/fetch/$s_!6tDJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png 848w, https://substackcdn.com/image/fetch/$s_!6tDJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png 1272w, https://substackcdn.com/image/fetch/$s_!6tDJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc76e3064-9a99-4b67-9a62-48138e9c8bde_1600x1200.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>Where High Reasoning Helps Most</h2><p>Looking by category, in this experiment, high reasoning shines where tasks are <strong>multi-step and format constrained</strong>:</p><ul><li><p>Analysis plans and analytics writeups</p></li><li><p>Communications with strict sections (for example, incident updates or executive summaries)</p></li><li><p>Structured planning and writing tasks</p></li><li><p>Accessible frontend components that combine behavior, structure, and constraints</p></li></ul><p>In these cases, model_3 often:</p><ul><li><p>Uses all required sections and ordering</p></li><li><p>Respects &#8220;only output X format&#8221; instructions</p></li><li><p>Covers more edge cases or reasoning steps that the other variants miss</p></li></ul><p>The category score lift plot makes this visible:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!levV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!levV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png 424w, https://substackcdn.com/image/fetch/$s_!levV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png 848w, https://substackcdn.com/image/fetch/$s_!levV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png 1272w, https://substackcdn.com/image/fetch/$s_!levV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!levV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png" width="570" height="570" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1456,&quot;width&quot;:1456,&quot;resizeWidth&quot;:570,&quot;bytes&quot;:91644,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thoughts.bitsnbytes.in/i/180418977?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!levV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png 424w, https://substackcdn.com/image/fetch/$s_!levV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png 848w, https://substackcdn.com/image/fetch/$s_!levV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png 1272w, https://substackcdn.com/image/fetch/$s_!levV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9264caae-1b46-4a7e-90b2-138af8762761_2000x2000.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Bars to the right are categories where high reasoning clearly adds value. For example, analysis, communication, planning, resume, and philosophy all see a strong lift.</p><h2>Where High Reasoning Falls Short</h2><p>High reasoning did <strong>not</strong> win everywhere.</p><p>On several categories that demand <strong>exactness</strong>:</p><ul><li><p>SQL queries</p></li><li><p>JSON schemas</p></li><li><p>Some Python snippets</p></li></ul><p>There are cases where model_3 produced a rich, well explained answer with one small syntax or logic error, while model_2 or model_1 produced something simpler but correct enough to win under the rubric.</p><p>Some patterns:</p><ul><li><p>In SQL related tasks, model_2 actually won slightly more tasks than model_3, even though model_3 had the best average score.</p></li><li><p>In schema related task, wins were roughly split between model_2 and model_3.</p></li><li><p>In a couple of writing tasks that demanded a very strict format, model_1 won because it simply followed the format while the higher reasoning variants tried to be more creative.</p></li></ul><p>The lesson is simple:</p><blockquote><p>Extra &#8220;thinking&#8221; can still introduce small mistakes on deterministic tasks where a missing comma or a wrong join breaks the whole thing.</p></blockquote><p></p><p><strong>Limitations of this exercise</strong>: I used the same model family (gpt-5.1) to grade, took only one sample per model per task, and had 98 tasks with some thin categories, so results are directional and do not account for cost or latency.</p><p>Results and code are posted here: <a href="https://github.com/Aravgit/reasoning_test">Reasoning Test</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thoughts.bitsnbytes.in/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Bits &amp; Bytes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Learnings from implementing Multi-Agent Systems & Sequential Workflows]]></title><description><![CDATA[Part of my learning experience to understand agentic systems and how Deep-Research works]]></description><link>https://thoughts.bitsnbytes.in/p/learnings-from-implementing-multi</link><guid isPermaLink="false">https://thoughts.bitsnbytes.in/p/learnings-from-implementing-multi</guid><dc:creator><![CDATA[Aravind]]></dc:creator><pubDate>Fri, 29 Aug 2025 13:25:49 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!rH2m!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I wanted to replicate <strong>deep research </strong>pipeline to learn more about agents for use cases like automating market research reports. Instead of relying on existing frameworks like Agents SDK, I decided to implement agents myself to understand the concepts better and also have control over state and storage.</p><h3>My Initial Multi-Agent Approach</h3><p>I built a single <strong>master orchestrator agent</strong> that analyzed incoming messages and routed them to specialized agents for:</p><ul><li><p>Web search</p></li><li><p>Tool calls</p></li><li><p>Analysis</p></li><li><p>Summary</p></li><li><p>Chat</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rH2m!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rH2m!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png 424w, https://substackcdn.com/image/fetch/$s_!rH2m!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png 848w, https://substackcdn.com/image/fetch/$s_!rH2m!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png 1272w, https://substackcdn.com/image/fetch/$s_!rH2m!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rH2m!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png" width="1456" height="1233" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1233,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:404124,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://thoughts.bitsnbytes.in/i/172168157?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rH2m!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png 424w, https://substackcdn.com/image/fetch/$s_!rH2m!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png 848w, https://substackcdn.com/image/fetch/$s_!rH2m!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png 1272w, https://substackcdn.com/image/fetch/$s_!rH2m!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae28c325-1d51-402f-aaa3-e19a90df1b68_3840x3251.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>This worked at first, but revealed some issues as the context grew:</p><ul><li><p>After 5&#8211;10 messages, the orchestrator started sending queries to the chat agent instead of the analysis agent, likely due to memory handoff issues. </p></li><li><p>The tool call accuracy dropped to 50-60% after 10 messages.</p></li><li><p>It would call the web search tool 3 times for the same question</p></li><li><p>The orchestrator struggled to keep track of what it was doing, a common limitation of simple master-agent setups without structured memory.</p></li><li><p>Despite extensive prompt engineering, the fundamental issues persisted.</p></li></ul><p></p><h3>Moving to Sequential Workflows</h3><p>To overcome these issues, I rebuilt the system with a <strong>deterministic, stage-based workflow</strong>:</p><ol><li><p>Clarification check - asks clarifying question after assessing user request</p></li><li><p>Topic generation - generates topics to analyze based on user query</p></li><li><p>Question generation - generates 5-10 questions related to the topic for the user query</p></li><li><p>Answer research - web search enabled LLM request to answer the question</p></li><li><p>Analysis on Data - LLM request to analyze data uploaded by user in context of the query.</p></li><li><p>Summary generation - Final summary generation covering all topics</p></li><li><p>Chat - Chat on the summary and data generated during the analysis</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZudF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZudF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png 424w, https://substackcdn.com/image/fetch/$s_!ZudF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png 848w, https://substackcdn.com/image/fetch/$s_!ZudF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png 1272w, https://substackcdn.com/image/fetch/$s_!ZudF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZudF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png" width="1456" height="2155" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2155,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:381071,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thoughts.bitsnbytes.in/i/172168157?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZudF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png 424w, https://substackcdn.com/image/fetch/$s_!ZudF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png 848w, https://substackcdn.com/image/fetch/$s_!ZudF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png 1272w, https://substackcdn.com/image/fetch/$s_!ZudF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc77db24-0dd9-4db2-94c0-30f912b5aa8b_2594x3840.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p><strong>Benefits of this architecture</strong>:</p><ul><li><p>Each stage is independent and resumable (if stage 3 fails, retry just that stage), with proper handoff and state</p></li><li><p>Defined Scope reduced hallucinations &#8212; each LLM call has one specific job.</p></li><li><p>Deterministic tool use &#8212; no agent confusion about whether to use tool or not, is defined by the stage.</p></li></ul><h3>Key learnings for complex workflows</h3><ul><li><p>No method is absolutely perfect: For well-defined workflows, let code enforce orchestration, retries, and state &#8212; while agents handle judgment tasks like clarification, research, and synthesis.</p></li><li><p>For reliability, make tool calls stage driven, not agent decided. Yes, this means unnecessary web searches when entering the "research" stage, but the trade-off is worth it for predictable, reliable behavior.</p></li><li><p>Agents work better when scope is staged narrowly and state is saved, so errors are contained and retries only affect that step, not the whole workflow.</p></li></ul><p><em>Next target: Implementing the same deep-research workflow using Agents SDK to explore structured memory, state persistence, and more reliable task handoff.</em></p><div><hr></div><p>Interested in reading more content on AI systems and startup journey? Subscribe to <a href="https://thoughts.bitsnbytes.in/">thought.bitsnbytes.in</a></p>]]></content:encoded></item><item><title><![CDATA[Speech Unleashed: Talk to AI in any language]]></title><description><![CDATA[How to build a voice-based custom LLM pipeline that works for any language and gives more accurate responses with fewer hallucinations]]></description><link>https://thoughts.bitsnbytes.in/p/building-jarvis-a-speech-to-speech-ai-agent-1dcd8f144075</link><guid isPermaLink="false">https://thoughts.bitsnbytes.in/p/building-jarvis-a-speech-to-speech-ai-agent-1dcd8f144075</guid><dc:creator><![CDATA[Aravind]]></dc:creator><pubDate>Tue, 26 Mar 2024 05:32:22 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/3823dffc-7c0c-4c07-8851-399334a90ee5_800x457.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Lj73!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Lj73!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Lj73!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Lj73!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Lj73!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Lj73!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:null,&quot;width&quot;:null,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Lj73!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Lj73!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Lj73!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Lj73!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd6da84f-7fff-4a93-8e4e-7e6bc26ad771_800x457.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a><figcaption class="image-caption">Image generated by Dall-E by OpenAI with a prompt from the&nbsp;author</figcaption></figure></div><h4><em>How to build a voice-based</em> <em>custom LLM pipeline that works for any language and gives more accurate responses with fewer hallucinations</em></h4><p>When I watched the <a href="https://www.hume.ai/">Hume AI</a> demo, I tested it for Indian languages,it got the emotion levels correct, but the output was gibberish in a few cases. I then tested the same with ChatGPT using voice mode, it worked for some questions and did not for some (eg: Fig 1).</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5qOG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5qOG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png 424w, https://substackcdn.com/image/fetch/$s_!5qOG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png 848w, https://substackcdn.com/image/fetch/$s_!5qOG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png 1272w, https://substackcdn.com/image/fetch/$s_!5qOG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5qOG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:null,&quot;width&quot;:null,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5qOG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png 424w, https://substackcdn.com/image/fetch/$s_!5qOG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png 848w, https://substackcdn.com/image/fetch/$s_!5qOG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png 1272w, https://substackcdn.com/image/fetch/$s_!5qOG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fadb5ea-092b-4335-9f71-c99f718792b4_691x557.png 1456w" sizes="100vw"></picture><div></div></div></a><figcaption class="image-caption"><strong>Fig 1: ChatGPT responses in Hindi using its voice&nbsp;mode</strong></figcaption></figure></div><p>I then tested translating the speech into English language and the responses were <a href="https://chat.openai.com/share/e/38d9d440-cad7-4cba-aac0-6fb99306137f">more accurate and with fewer hallucinations</a>. To automate this process, I started building a pipeline that translates any language to English and answers the user question in the same source language. This pipeline can be used in a voice-based RAG system, where user questions are queried on a custom knowledge base.</p><p>This article explains the main elements and code required to build this pipeline. Most of the components used here are open-source models (except for the GPT 3.5 turbo model used as LLM) making it easy to replicate. This article assumes you understand the terms large language model (LLM), Retrieval Augment Generation (RAG), RAG frameworks like Llama-index, and Vector database (like Qdrant) and their functionality.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rmVa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rmVa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png 424w, https://substackcdn.com/image/fetch/$s_!rmVa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png 848w, https://substackcdn.com/image/fetch/$s_!rmVa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png 1272w, https://substackcdn.com/image/fetch/$s_!rmVa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rmVa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:null,&quot;width&quot;:null,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Image showing flow of the tool, explaining how voice recorded is translated using Meta Seamless M4T and then LLM answers the user question, and responds back in users source language&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Image showing flow of the tool, explaining how voice recorded is translated using Meta Seamless M4T and then LLM answers the user question, and responds back in users source language" title="Image showing flow of the tool, explaining how voice recorded is translated using Meta Seamless M4T and then LLM answers the user question, and responds back in users source language" srcset="https://substackcdn.com/image/fetch/$s_!rmVa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png 424w, https://substackcdn.com/image/fetch/$s_!rmVa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png 848w, https://substackcdn.com/image/fetch/$s_!rmVa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png 1272w, https://substackcdn.com/image/fetch/$s_!rmVa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf95385-90bc-48a0-8d83-ffe0b87a4c67_800x368.png 1456w" sizes="100vw"></picture><div></div></div></a><figcaption class="image-caption"><strong>Fig 2: Answer the voice questions using LLMs and covert back to&nbsp;speech</strong></figcaption></figure></div><p>TL; DR version: The input is a speech recording, which is sent to the Seamless M4T model for accurate translation to English. Once translated it is sent to LLM answering the question asked. The response is converted to audio of the source language and played to the user. The entire process can be considered as a 3 step process.</p><ul><li><p>Step 1: Pre-process the recording into specific dimensions for speech translation to work. Send the request to translate the recording using M4T models and identify the source language using Whisper models.</p></li><li><p>Step 2: Send the translation to query on the knowledge base (a vector database) to retrieve the required information and then send the text to LLM to answer the question.</p></li><li><p>Step 3: Translate the answer to the source language and play the audio output to the user.</p></li></ul><p>Let's start building&#8230;&#8230;&#8230;.</p><h4>Translating the speech using Seamless M4T&nbsp;models</h4><p>The first element of the pipeline is translating the question into English. This is done because LLM response accuracy is higher when the language is English. To translate the speech into English, we use Meta&#8217;s <a href="https://colab.research.google.com/drive/1gIyG8AUtc0en-aIF2JbJQMo7tRV404W5">Seamless M4T</a>.</p><p>The first step is to convert the sample rate to 16000 Hz using the librosa library as Seamless M4T models are trained on a sample rate of 16000 Hz.</p><pre><code>import librosa

def adjust_speed_librosa(audio_path, output_path, speed_factor):
    y, sr = librosa.load(audio_path, sr=None)
    y_fast = librosa.effects.time_stretch(y,rate= speed_factor)
    sf.write(output_path, y_fast, sr)</code></pre><p>Then we send the resampled recording file for translation. We set up a Python application using fastapi to host the m4t model on a GPU using the script m4t_app.py (shared at the end of the article) and this application can process our requests.</p><p>Seamless M4T being multimodal, can convert text to speech or text, and similarly speech to speech or text. Source language is required as input for text translations, for speech translations it is not required. For this article, we will focus on speech as input.</p><pre><code>def send_file_for_translation(input_data, mode, tgt_lang, src_lang):
    if mode == "t2st" or mode == "t2tt":  # text to speech translation or text to text translation
        data = {
            'input_data': input_data,  # this is the text input
            'mode': mode,
            'tgt_lang': tgt_lang,
            'src_lang': src_lang
        }
        response = requests.post(API_URL, data=data)

    else:  # mode == "s2tt" for speech to text translation
        with open(input_data, 'rb') as f:  # here input_data is the file path
            files = {'file': (input_data, f, 'audio/wav')}
            data = {
                'mode': mode,
                'tgt_lang': tgt_lang,
                'src_lang': src_lang
            }
            response = requests.post(API_URL, files=files, data=data)
    response_json = response.json()  # Parse the JSON response
    if mode in ["s2tt", "t2tt"]:
        return response_json.get('translated_text', None)
    elif mode == "t2st":
        return response_json.get('audio_link', None)</code></pre><p>Behind the scenes, when the request is sent, first the seamless m4t model is used to translate the text. In the later steps of the process, we convert the output to speech, which requires the source language. Seamless M4T does not have built-in <a href="https://github.com/facebookresearch/seamless_communication/issues/206">source language detection</a>. We use OpenAI&#8217;s Whisper model to get the source language (why not use the Whisper model for both translation and source language detection, story for another time). Both the translated content and the source language are sent back as the response.</p><pre><code>async def translate_audio(file: UploadFile = File(None),
                          input_data: str = Form(None), 
                          mode: str = Form(...),
                          tgt_lang: str = Form(...),
                          src_lang: str = Form(None),
                         model:str = Form(None)):
    
    if mode == "s2tt" and file:  # speech to text translation and file is provided
        temp_file = _save_temp_file(await file.read())
        input_path = temp_file
    elif mode == "t2st" and input_data:  # text to speech translation and text is provided
        input_path = input_data
    elif mode == "t2tt" and input_data:
        input_path = input_data
    else:
        raise ValueError("Invalid mode or input not provided!")

    # Set up arguments for m4t_predict
    output_filename = "output_t2st.wav" 
    if mode in ['t2st','s2tt']:
        if mode == "t2st":
            output_filename = "output_t2st.wav" 
        elif mode == "s2tt":
            output_filename = f"output_{file.filename}"
        args = Namespace(
            input=input_path,
            task=mode,
            tgt_lang=tgt_lang,
            src_lang=src_lang,
            output_path=os.path.join(UPLOAD_DIR, output_filename),
            model_name="seamlessM4T_large",
            vocoder_name="vocoder_36langs",
            ngram_filtering=False)
    else:
        args = Namespace(
            input=input_path,
            task=mode,
            tgt_lang=tgt_lang,
            src_lang=src_lang,
            model_name="seamlessM4T_large",
            vocoder_name="vocoder_36langs",
            ngram_filtering=False)


    # Call the prediction function
    translated_text = m4t_predict(args)

    if (mode == "s2tt"):
        model_w = whisper.load_model("large")
        
        audio = whisper.load_audio(input_path)
        audio = whisper.pad_or_trim(audio)

        # make log-Mel spectrogram and move to the same device as the model
        mel = whisper.log_mel_spectrogram(audio).to(model_w.device)
        # detect the spoken language
        _, probs = model_w.detect_language(mel)
        print(f"Detected language: {max(probs, key=probs.get)}")
        src_lang = {max(probs, key=probs.get)}
        if file:
            os.remove(temp_file)
        
        return {"translated_text": translated_text,
                'src_lang' :src_lang} 
    elif mode == 't2tt':
        if file:
            os.remove(temp_file)
        return {"translated_text": translated_text} 
        # Use translated_text
    else:  # mode == "t2st"
        print(f"./{os.path.basename(args.output_path)}")
        return {"audio_link": f"./{os.path.basename(args.output_path)}"}  # Use args.output_path</code></pre><h4><strong>Use RAG to add information to answer the translated question</strong></h4><p>Once we translate it to text, the next step is to send the question to an LLM along with the required information. This can be achieved by setting up a RAG system using frameworks like Llama-Index, Langchain, or even the OpenAI assistants. Retrieval Augmented Generation (RAG) system is a framework that combines LLMs with a private knowledge base and queries the required information for a specific input/question. I have used <a href="https://docs.llamaindex.ai/en/stable/">Llama-index</a> using the <a href="https://qdrant.tech/documentation/quick-start/">Qdrant </a>vector database adding text data related to a business. The translated question is sent as text input. Llama-index calculates the cosine similarity of the vectors, ranks the vectors, and sends the top k nodes to the LLM to answer the question.</p><pre><code>def get_llm_response(question):
    count = 3
    mode = 'compact'
    async_mode=False
    llm_predictor = LLMPredictor(AzureChatOpenAI (deployment_name='gpt-35-turbo',model='gpt-3.5-turbo', 
                                             temperature=0,max_tokens=256,
                                             openai_api_key=openai.api_key,openai_api_base=openai.api_base,
                                             openai_api_type=openai.api_type,openai_api_version='2023-05-15',
                                             ))
    embeddings = LangchainEmbedding(OpenAIEmbeddings( deployment="text-embedding-ada-002",model="text-embedding-ada-002",
                                                    openai_api_base=openai.api_base,openai_api_key=openai.api_key,
                                                    openai_api_type=openai.api_type,openai_api_version=openai.api_version),
                                    embed_batch_size=1,)    
    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor,embed_model=embeddings)

    client = qdrant_client.QdrantClient(url=f"{hostname}:6333")
    vector_store = QdrantVectorStore(client=client, collection_name=f"collection_name")
    index = VectorStoreIndex.from_vector_store(vector_store=vector_store,service_context=service_context)
    response_synthesizer = get_response_synthesizer(response_mode=mode, use_async=async_mode,service_context=service_context, text_qa_template = SYSTEM_MESSAGE)
    query_engine = index.as_query_engine(response_synthesizer=response_synthesizer,similarity_top_k=count))
    response = query_engine.query(question)
    return response.response</code></pre><h4>Convert the text to speech using Google text to speech synthesizer</h4><p>The last part of the pipeline is converting the text answer from the LLM to speech. The answer in English from the previous step (a text output) is sent to the translation model to convert it into source language text (Another way to achieve this is to add instructions to the LLM to answer it in the source language). We can skip this step if the source language is English.</p><p>We use Google&#8217;s gTTS library, which can convert text to speech when the language is specified. We use the source language detected (by OpenAI's Whisper model) and convert the text into speech, which is played to the user.</p><pre><code>def text_to_audio(text, lang=gtts_lang_input):
    """Converts given text to audio and plays it."""
    tts = gTTS(text=text, lang=lang, slow=False)
    tts.save("output_audio.mp3")
    return ipd.Audio("output_audio.mp3")</code></pre><h4>Things to consider while deploying this&nbsp;pipeline</h4><p>We now have a complete pipeline where users can ask a question and we can answer the question in the same source language. Few things to consider here in the pipeline:</p><ul><li><p>Both Seamless M4T and Whisper models are available in different sizes. The accuracy of the model and the time required to process the input changes based on the size of the model selected. So, a lot of experiments are required based on the use case.</p></li><li><p>gTTS was used instead of M4T for text-to-speech because the speech output from the model, as it required post-processing. gTTS model&#8217;s output was clear and did not require further processing.</p></li><li><p>Seamless M4T is multi-modal, it can work for S2ST, S2TT, T2ST, and T2TT, so we need to mention the input type for the model. It can translate 100 languages and it&#8217;s an open-source model.</p></li><li><p>This article is based on Seamless M4T models (released in Aug 2023), since I have drafted this article (and not published for a long time), Meta has released the V2 model, which has similar functionality but improved accuracy.</p></li><li><p>The code is based on older versions of llama-index and OpenAI. Both packages have had major breaking changes since then. I&#8217;ve included the versions in the requirements file.</p></li></ul><p>I&#8217;ve shared all the code I&#8217;ve used in this <a href="https://github.com/Aravgit/Speech_toLLM_to_Speech">git repo</a></p>]]></content:encoded></item></channel></rss>