<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><link href="https://victorangeloblancada.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://victorangeloblancada.github.io/" rel="alternate" type="text/html" /><updated>2026-08-06T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/feed.xml</id><title type="html">Victor Angelo Blancada</title><subtitle>Victor Angelo Blancada is an AI, data science, and optimization leader with 12+ years of experience building analytics capabilities, decision-support systems, and teams across global logistics, telecommunications, consulting, and marketing.</subtitle><entry><title type="html">The Seams Are the Strategy</title><link href="https://victorangeloblancada.github.io/blog/2026/08/06/power-bi-arch-not-macos.html" rel="alternate" type="text/html" title="The Seams Are the Strategy" /><published>2026-08-06T00:00:00.000Z</published><updated>2026-08-06T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/08/06/power-bi-arch-not-macos</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/08/06/power-bi-arch-not-macos.html"><![CDATA[<p>There is a small, stupid, perfect bug in Power BI Desktop that explains almost everything about Microsoft.</p>
<p>Put two fingers on a trackpad and pinch. The interface obligingly zooms — not the report canvas, which would be sensible, but the <em>chrome</em>: the ribbon, the panes, the furniture of the application itself. Keep going and the window’s own close button drifts serenely past the edge of the window and vanishes. You have zoomed the application out of its own frame. The escape hatch has escaped.</p>
<p>This is not a defect in the ordinary sense. Nobody specified it. Nobody chose it. It is an <em>emergent property</em> — the observable consequence of two layout systems occupying one window with no treaty governing which of them is in charge. The native Windows shell measures in device-independent units and negotiates DPI with the operating system. The embedded Chromium runtime measures in CSS pixels and regards a pinch gesture as a page zoom it is fully entitled to apply. When you pinch, Chromium scales its content, the host window does not resize, and the surplus is simply clipped at a boundary neither party knew was contested.</p>
<p>Microsoft did not build a seamless application with an unfortunate crack in it. Microsoft built a federation and shipped the treaty negotiations to production.</p>
<p>Once you see this, you cannot unsee it, and the entire product reorganizes itself in your mind. Every idiosyncrasy that felt like neglect turns out to be a load-bearing wall.</p>
<hr>
<h2 id="i-an-archaeology-of-the-exe">I. An archaeology of the .exe</h2>
<p>Power BI Desktop is commonly described as an application. It is more accurate to describe it as a <em>building</em> — one erected over fifteen years by four contractors who never met, on foundations poured for an entirely different structure.</p>
<p>The oldest stratum is the engine. Around 2006, a project codenamed <strong>Gemini</strong> under Amir Netz set out to take SQL Server Analysis Services and make it run in memory. It surfaced publicly in 2010 as Power Pivot, a free Excel add-in, and it brought two things with it that remain the load-bearing pillars of the entire Microsoft analytics business: <strong>VertiPaq</strong>, a columnar compression engine of genuinely remarkable efficiency, and <strong>DAX</strong>, a formula language that resembles Excel’s the way a submarine resembles a bathtub.</p>
<p>The second stratum is the canvas. <strong>Project Crescent</strong>, previewed at PASS Summit in 2010 and shipped as Power View inside SQL Server 2012, was Microsoft’s answer to Tableau: interactive filtering, cross-highlighting, animated scatter plots. Its DNA is still visible every time a visual cross-filters its neighbours.</p>
<p>The third is the plumbing. <strong>Data Explorer</strong>, another Excel add-in, arrived in 2013 and was renamed Power Query. It brought M, a functional language for data preparation, and the Mashup engine that executes it.</p>
<p>Three separate add-ins, three separate teams, three separate release cadences. In 2014 Microsoft put them in a single window, called it Power BI Designer Preview, renamed it Power BI Desktop, and shipped GA on 24 July 2015.</p>
<p>Here is the crucial detail, and it is the one that explains the next decade: <strong>they were never merged. They were co-located.</strong> Power BI Desktop is a shared window with a shared ribbon over three engines that remain, to this day, architecturally distinct processes and codebases with distinct protocols. Launch Desktop and it silently spins up a local instance of <code>msmdsrv.exe</code> — an actual Analysis Services server, listening on a random port, hosting your model under a random GUID. Every visual you render fires a DAX query at it. Your laptop is running a database server and neither of you talks about it.</p>
<p>The report canvas, meanwhile, is Chromium. Microsoft migrated it from CefSharp to WebView2 across 2021–22, for reasons that were entirely defensible: inherit the Edge team’s security patches automatically, stop hand-carrying browser updates, redirect the saved engineering hours into features.</p>
<p>The bill for that decision came due in January 2026, when an external WebView2 update — shipped by a different team, on a different schedule, to machines that had not updated Power BI at all — rendered a swathe of Desktop’s icons as solid black blobs. Marco Russo’s published workaround was to pin an older WebView2 build via an environment variable, with a note reminding readers to check weekly whether they could stop. Part of your business intelligence tool is versioned by the browser team, and you find out on a Tuesday.</p>
<hr>
<h2 id="ii-the-map-menu-is-an-org-chart">II. The map menu is an org chart</h2>
<p>Conway’s Law — organizations ship their communication structure — is usually invoked as gentle sociological color. In Power BI it is closer to a forensic tool. You can read the org chart directly off the visualization pane.</p>
<p>Open the map options and count. There is <strong>Map</strong> and <strong>Filled Map</strong>, descendants of Power View, geocoded by Bing. There is <strong>Azure Maps</strong>, built by the Azure Maps team, which requires a tenant administrator to enable it under Integration settings, because of course it does. There is <strong>ArcGIS for Power BI</strong>, built by Esri, governed by Esri’s terms and privacy policy, with its own consent dialog and its own release changelog on Esri’s community forum. And there is <strong>Shape Map</strong>, which renders TopoJSON without touching a service at all and which reached general availability in June 2026, roughly a decade after it entered preview.</p>
<p>Four ways to put a dot on a map. They differ almost entirely in <em>who wrote them</em>.</p>
<p>The obvious question is why not simply consolidate? The answer is instructive, and it is not laziness. Bing Maps is officially scheduled for deprecation — with, in Microsoft’s own careful phrasing, a timeline that has not yet been determined. It cannot be switched off, because Azure Maps does not yet serve China, Korea, or government clouds, and Microsoft’s documentation explicitly instructs users in those regions to <em>stay on the deprecated visual</em>. A visual can be simultaneously dead and mandatory. It can be sunset and load-bearing.</p>
<p>So the four maps persist, and Microsoft does what a large organization with a backward-compatibility covenant does instead of deleting things: it ships a migration dialog. Open a report with legacy maps and Desktop offers to upgrade them all at once, carrying settings across, with a gentle note that some circles may come out smaller because Azure Maps caps marker size lower than Bing capped bubble size. This is the physical sensation of technical debt being serviced rather than repaid.</p>
<hr>
<h2 id="iii-redundancy-that-isnt">III. Redundancy that isn’t</h2>
<p>The complaint that Power BI offers too many ways to do the same thing is nearly universal and almost entirely wrong. It is a category error, and the category is <em>time</em>.</p>
<p>You can build a table in Power Query, or you can build one in DAX. These look interchangeable and are not, because they execute at different moments in a pipeline and therefore see different worlds. Power Query runs at refresh, before the data lands, speaking to the source — which means it can push work back into your SQL server, and which means it cannot see a relationship that does not exist yet. DAX runs at query time, inside the model, with full visibility of relationships and filter context — which means it can do time intelligence and context transition, and which means it cannot fold a predicate into your source.</p>
<p>The rule that falls out of this is elegant, and SQLBI has been repeating it for years: use a DAX calculated column only when the answer depends on <em>other rows or other tables</em>; otherwise use Power Query and pay the cost once. Everything else — dataflows, lakehouse SQL, visual calculations, and the DAX user-defined functions that reached GA in June 2026 — is the same principle at additional altitudes.</p>
<p>The redundancy is not in the engine. It is in the <em>pedagogy</em>. Microsoft has never told anyone this clearly, because telling people clearly would require admitting that the seamless application is actually a pipeline with stages, and the stages have rules, and knowing the rules is now your job.</p>
<hr>
<h2 id="iv-the-annexed-province">IV. The annexed province</h2>
<p>Paginated reports feel bolted on because they are, in the most literal available sense, a different product wearing the same lanyard.</p>
<p>RDL — Report Definition Language — has been part of the Microsoft stack since SQL Server 2000 Reporting Services. When Microsoft says “paginated reports in Power BI,” it means .rdl files executing inside a cloud workspace instead of an on-premises SSRS server. <strong>The infrastructure changed. The rendering engine did not.</strong> Different file format, different authoring tool, different expression language, different data model, different licensing gate.</p>
<p>And it exists for an entirely respectable reason. The interactive canvas is a fixed-size, screen-first surface that fundamentally cannot paginate, and some reporting genuinely is not a dashboard: the forty-page invoice, the financial statement where every decimal lands in a fixed column, the regulatory filing where the auditor needs identical pagination across quarters. Microsoft needed that capability, already owned a mature engine for it, and had decided that SQL Server 2022 would be SSRS’s last release. Annexation was the rational move.</p>
<p>But annexation leaves borders, and the borders are where migration teams bleed. Document Map does not render in the service. SSRS subscriptions do not transfer and must be rebuilt. Linked reports must live in the same workspace. Convergence is real but incremental — Power Query Online reached GA in Report Builder in 2024, browser-based paginated authoring has landed, a Power Query discovery UI arrived in June 2026, CRUD REST APIs in July. This is not neglect. It is a border being slowly dissolved by a team that must not break a single one of the ten thousand invoices already running across it.</p>
<hr>
<h2 id="v-i-use-arch-power-bi-btw">V. I use <del>Arch</del> Power BI, BTW</h2>
<p>Here is where the story turns, because the obvious prescription — <em>Microsoft should expose the modularity instead of pretending it’s one seamless app</em> — turns out to have already happened. Almost nobody noticed.</p>
<p>The model layer is not merely open; it has been open for a quarter century. Semantic models speak <strong>XMLA</strong>, a specification Microsoft published jointly with Hyperion in 2001. That is why a third-party ecosystem worked on day one without anyone’s permission. The External Tools ribbon exists for the sole purpose of handing a registered tool the port number and model name of that local Analysis Services instance — registration being a JSON file dropped in a folder. In the cloud, the XMLA endpoint does the same for published models, and since 9 June 2025 all Fabric capacities default to read/write.</p>
<p>Tabular Editor, connected through this door, can configure object-level security, calculation groups, and partition strategies that <strong>Power BI Desktop’s own interface cannot</strong>. The community tool is more capable than the first-party one, at the layer where capability matters most.</p>
<p>The report layer got the same treatment, belatedly and deliberately: TMDL for the model as plain text, PBIR storing every visual and page and bookmark as an individually schema’d JSON file — <em>publicly documented, explicitly designed to be modified by non-Power BI applications</em> — and PBIP as the project container. The Fabric CLI was open-sourced under MIT in October 2025 with a filesystem metaphor. <code>semantic-link-labs</code> lives under the <code>microsoft/</code> GitHub organization. In June 2026 Microsoft shipped a <strong>Desktop Bridge</strong>: a JSON-RPC server running inside the Desktop process, over a named pipe, so agents can read report state, reload from disk, and screenshot themselves for verification.</p>
<p>So the modularity is real, documented, and in several places open source. Which raises the actual question: why does nobody know?</p>
<p>Consider a scene reported by a consultant in Australia: a senior analyst at a bank, hand-authoring eighty measures one at a time through the Desktop properties pane — roughly an hour’s work in Tabular Editor’s scripting console — because she had never heard of Tabular Editor. Neither had anyone on her team. They had all completed a five-day official Power BI course in which external tools were not mentioned.</p>
<p>That is not an accident of curriculum. It is the strategy, expressed as an omission.</p>
<p>Microsoft’s own documentation states plainly that it does not provide support or documentation for external tools except where an issue reproduces in Microsoft’s own. The ecosystem is <em>permitted</em>, not <em>warranted</em>. And the sharpest tell of all: a model modified through the XMLA endpoint <strong>can no longer be downloaded to Power BI Desktop</strong>. Walk through the open door and it locks behind you. There is no clearer statement of institutional ambivalence than an interface you are welcome to use exactly once.</p>
<p>The pattern, once stated, is almost embarrassingly legible. <strong>Microsoft opens what drives consumption and closes what drives acquisition.</strong> The model engine, the CLIs, the REST APIs — all of these increase capacity utilization, which is metered, which is revenue. The Windows-only Desktop, the visual gallery, the drag-and-drop onboarding — these are the funnel, and funnels are not for opening.</p>
<p>It is not that Power BI is a monolith masquerading as modular. It is closer to a Linux distribution that ships a locked-down desktop environment while the package manager sits fully functional, fully documented, and entirely unmentioned in the getting-started guide. The remedy was never re-architecture. It was a sentence in the docs.</p>
<hr>
<h2 id="vi-we-have-power-bi-at-home">VI. We have Power BI at home</h2>
<p>Which invites the natural experiment: if the components are separable, can you separate them entirely? Assemble a stack from open-source parts and see how much of Power BI reappears?</p>
<p>The answer is roughly eighty-five to ninety percent, and the shape of the residual is the interesting part.</p>
<p>Ingestion falls immediately — dlt, Meltano, Airbyte, hundreds of connectors. Transformation doesn’t just fall, it <em>improves</em>: dbt and SQLMesh give you tested, version-controlled, lineage-aware logic, which is more than M has ever offered. Compute is DuckDB, MIT-licensed, columnar-vectorized, embedded, no server, reads Parquet directly, and — the detail that quietly matters most — compiles to WebAssembly and runs in a browser tab. Orchestration is Dagster, which makes Power BI’s refresh scheduler look like a kitchen timer. Paginated output is WeasyPrint or Typst driving CSS Paged Media, a layout system both better documented and more expressive than RDL. Interactive reporting is Superset or Metabase or Lightdash; narrative reporting is Evidence.dev, which is frankly what Power BI should have shipped for anything requiring a paragraph.</p>
<p>The semantic layer is Cube, and Cube is the load-bearing choice, because it is the only open-source option that both <em>defines and serves</em> metrics: standalone server, its own pre-aggregation store, native row-level security and multi-tenancy, and endpoints for REST, GraphQL, SQL, MDX — and DAX.</p>
<p>Hold that last one. It matters in a moment.</p>
<p>Because there is exactly one thing in this stack that cannot be reconstructed, and it is not the file format. Reverse engineering has, remarkably, gotten <em>inside</em> VertiPaq: <code>pbixray</code> decodes the full chain — XPRESS9 into an Analysis Services backup file, then Huffman dictionaries, then run-length encoding, then bit-packing — and reads .pbix, PowerPivot .xlsx, and .abf files under an MIT license with no Microsoft software installed. The vault is open.</p>
<p>What nobody has built is an engine that <em>evaluates DAX</em>. The best available parser announces in its own documentation that it is designed for introspection and analysis, <strong>not execution</strong>. A DAX-to-SQL translator handles <code>FILTER</code> and <code>SUMMARIZE</code> and <code>CALCULATETABLE</code> and then stops, because the thing it would need to cross is not a function.</p>
<p><code>CALCULATE</code> is not a function. It is a context-manipulation primitive, and filter context, row context, and context transition have no SQL analogue. Twenty years of Microsoft’s compounding investment sits in that one keyword, and it is the reason a company can open its file format, its wire protocol, its CLI, its report schema, and its process boundary without losing anything that matters.</p>
<p><strong>The moat was never the software. It was the semantics.</strong></p>
<p>The other irreducible gap is less technical and more humbling: self-service authoring. Power BI’s genuine achievement — the one no FOSS stack replicates — is that a supply chain planner with no engineering background can build a working report between lunch and the four o’clock. Metabase’s question builder is the closest analogue and it is not as good. Power BI’s real product is not capability. It is <em>accessibility</em>, and accessibility is extraordinarily expensive to manufacture.</p>
<hr>
<h2 id="vii-portability-is-not-a-separate-project">VII. Portability is not a separate project</h2>
<p>Ask whether Power BI can be ported to Linux or macOS and you receive an unusually unambiguous no. Desktop is WPF plus WebView2 plus a local Analysis Services instance, and Analysis Services has never shipped for Linux at all. Wine and CrossOver do not carry it. A Mac feature request accumulated something on the order of four thousand votes and was formally declined. Parallels, VMware, or a cloud Windows desktop are the entire menu. The mobile apps are consumption-only. The Windows UWP app was retired at the end of 2023, which means the most Microsoft-native client of all is the one that died first.</p>
<p>Now note what happened to that question when the stack was decomposed. DuckDB, dbt, dlt, Cube, Superset, Metabase, Evidence, WeasyPrint, Dagster — every one of them runs natively on Linux, macOS, and Windows. Most are containerized. Several run in a browser.</p>
<p>The portability problem and the emulation problem are not two problems. <strong>They are one problem, and it is solved by the same act.</strong> Nobody needs to port Power BI. They need to replace the single layer that refuses to be portable, and that layer is the Windows shell — which is to say, the funnel.</p>
<hr>
<h2 id="viii">VIII.</h2>
<p>Everything above converges on a single strategic observation, and it inverts the usual framing.</p>
<p>The instinctive response to a platform whose seams show is to demand that the vendor fix the seams. That demand will not be met, and it should not be. Microsoft’s incentives are legible and stable: open what is metered, close what acquires, freeze what is published, and never, under any circumstance, break the invoice that has been running since 2016. A firm optimizing for those constraints produces exactly the artifact we have — a federation with excellent bones, honest protocols, and no narrative.</p>
<p>The better response is to stop treating Power BI as a platform and start treating it as <strong>a client</strong>.</p>
<p>This is what the Cube DAX endpoint is quietly for. Stand up a semantic layer you control, define your metrics there in text under version control, and then point Power BI <em>at it</em>. Your analysts keep the authoring experience that took them four years to learn. Your metric definitions leave the binary and enter Git, where they can be reviewed, tested, diffed, and argued about in a pull request like every other consequential piece of business logic. And the front end becomes what it should always have been: a rendering choice, revisable next quarter, rather than an architecture decision you made in 2017 and have been living inside ever since.</p>
<p>Notice what this does to the DAX moat. You cannot cross it — nobody can — but you can <em>decline to build your city on the far bank</em>. The irreplaceable thing about DAX is its evaluation semantics, not the business logic those semantics happen to encode. Move the logic; leave the language.</p>
<p>The five idiosyncrasies we started with are therefore not a list of defects. They are a map of where a very large organization has drawn its boundaries, and boundaries drawn by someone else’s incentives are precisely the boundaries you should not organize your own work around. The pinch-to-zoom bug is not a metaphor for anything. It is simply what happens when two systems share a window and neither one owns the frame.</p>
<hr>
<p>It remains the case that the close button is still, technically, out there somewhere. Alt+F4 works. It always has. That is arguably the most Microsoft sentence in this entire piece: the escape hatch exists, it is fully documented, it has worked identically since 1985, and nobody thought to mention it.</p>
<p><em>Victor Blancada is a data scientist focused on deriving actionable insights for clients. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="data science" /><category term="power bi" /><category term="software development" /><summary type="html">What Power BI's weirdest bugs reveal about how Microsoft actually builds things — and why the fix isn't a better product, it's a better posture.</summary></entry><entry><title type="html">When the Bell Curve Bends: Lean Six Sigma in the Age of Machine Intelligence</title><link href="https://victorangeloblancada.github.io/blog/2026/07/31/lean-six-sigma-ai.html" rel="alternate" type="text/html" title="When the Bell Curve Bends: Lean Six Sigma in the Age of Machine Intelligence" /><published>2026-07-31T00:00:00.000Z</published><updated>2026-07-31T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/07/31/lean-six-sigma-ai</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/07/31/lean-six-sigma-ai.html"><![CDATA[<p>In classical physics, Newtonian mechanics described a deterministic universe—a world of point-like certainties, where forces applied to massive bodies yielded predictable, linear trajectories. But when physicists turned their lenses to the subatomic realm, classical equations collapsed. They were forced to transition to quantum mechanics: a framework defined by wave functions, probability distributions, and the unsettling truth that measuring a state alters it.</p>
<p>A similar paradigm shift is quietly unfolding within operational excellence. Twentieth-century Lean Six Sigma was the Newtonian mechanics of business process design. Forged in the high-volume, physical manufacturing lines of Motorola, Toyota, and General Electric, it assumed a world of deterministic, observable tasks. Processes were staffed by human operators whose cycle times followed stable Gaussian distributions, and variations were treated as mechanical errors to be systematically eradicated.</p>
<p>However, the moment we place an artificial intelligence inside the value stream, this classical worldview collapses. AI is not merely faster automation; it is a stochastically operating cognitive agent. The output of an LLM is not a deterministic point on a real line, but a probability distribution over a high-dimensional token space, sampled stochastically. We have transitioned from the Newtonian world of predictable machine tolerances and simple human actions to the quantum world of probabilistic cognitive agents. In this new regime, old heuristics do not just fail; they actively mislead.</p>
<p>This guide is not an obituary for Lean Six Sigma. It is an argument that the discipline must be re-derived from its foundations to accommodate a new kind of worker: one that produces knowledge outputs at a marginal cost measured in tokens rather than hours, fails in probabilistic and sometimes novel ways, decomposes goals into recursive sub-processes when configured as an agent, and forces the binding constraint of any process to migrate from human production to a combination of human verification, machine iteration, and inference spend. The soul of the methodology—reduce variation where it destroys value, eliminate waste where it accrues, respect the customer, and honor the data—remains intact. The mental models used to operationalize it are ripe for reconstruction.</p>
<hr>
<h2 id="part-i-what-lean-six-sigma-actually-was-beneath-the-vocabulary">Part I: What Lean Six Sigma Actually Was, Beneath the Vocabulary</h2>
<p>Before we can identify which rules of thumb have outlived their usefulness, we must recover what the discipline was originally trying to accomplish. Strip away the belts, the acronyms, and the ceremonial DMAIC diagrams, and Lean Six Sigma reduces to a handful of remarkably durable first principles:</p>
<ul>
<li><strong>Quality is an economic phenomenon, not a moral one:</strong> Joseph Juran’s cost-of-quality framework decomposed total quality expenditure into prevention, appraisal, internal failure, and external failure. Every managerial choice trades among these buckets, seeking to minimize their sum. Juran’s decomposition survives the transition to AI with almost eerie fidelity—the four buckets simply refill with new contents, and the currency partially shifts from dollars to tokens.</li>
<li><strong>Variation is the enemy of predictability:</strong> Predictability is the currency of any promise made to a customer. Walter Shewhart’s control charts and W. Edwards Deming’s focus on distinguishing common-cause from special-cause variation were mechanisms for policing this predictability.</li>
<li><strong>Waste is anything the customer would not knowingly pay for:</strong> Taiichi Ohno famously operationalized this through his taxonomy of the seven (later eight) wastes.</li>
<li><strong>Improvement is empirical and iterative:</strong> Embodied in the Plan-Do-Study-Act (PDSA) cycle and its industrialized descendant, DMAIC.</li>
</ul>
<p>Each of these principles was translated, over decades, into practical heuristics. Some were quantitative: aim for 3.4 defects per million opportunities; compute takt time by dividing available production time by customer demand; keep the process capability index (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>C</mi><mi>p</mi></msub></mrow><annotation encoding="application/x-tex">C_p</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9694em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0715em;">C</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0715em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">p</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span></span></span></span>) above 1.33. Others were qualitative: standardize before you improve; go and see (<em>genchi genbutsu</em>); ask “why” five times.</p>
<p>These heuristics performed magnificently when the underlying process resembled the one in which they were derived. Funnily enough, they begin to mislead when the substrate changes—and the substrate is now changing profoundly, along three axes at once: the producer has become probabilistic; the producer, when configured as an agent, has become a <em>process</em> in its own right, capable of decomposing goals and calling tools in recursive loops; and the marginal cost of production, though small, is neither zero nor uniform, and now traces a variable cost curve that Lean practitioners had almost forgotten how to reason about.</p>
<hr>
<h2 id="part-ii-the-rules-of-thumb-that-have-quietly-expired">Part II: The Rules of Thumb That Have Quietly Expired</h2>
<h3 id="1-reducing-variation-always-improves-quality">1. “Reducing variation always improves quality”</h3>
<p>The Six Sigma quality target of 3.4 defects per million opportunities (DPMO) was calibrated for processes whose outputs were discrete, inspectable, and normally distributed around a specification. A machined shaft is either within tolerance or it is not. The distribution of shaft diameters is well approximated by a Gaussian, and the tails are thin enough that reducing standard deviation reliably reduces the probability of exceeding specification limits.</p>
<p>Artificial intelligence violates every one of these assumptions. The output of a large language model is not a point on a real line but a probability distribution over a vast token space, sampled stochastically by design. The failure modes are heavy-tailed and often novel: a system that behaves competently across a million prompts may produce a catastrophic hallucination on the million-and-first, and that hallucination will not have been drawn from a Gaussian tail because the underlying error distribution is not Gaussian.</p>
<p>Worse, reducing the “variation” of an AI output—for example, by lowering temperature to zero—does not necessarily improve quality; it may collapse the model onto a confidently wrong mode, trading beneficial diversity for consistent error. Statistical process control (SPC) assumed that the process was stationary and that its noise was well-characterized. Neither assumption survives contact with a stochastic function approximator whose behavior on out-of-distribution inputs is, in the strict mathematical sense, undefined.</p>
<p>The situation detoriorates further when the AI is configured as an agent. A single-shot model has one opportunity to err; an agent that decomposes a goal into ten dependent tool calls has ten opportunities. If a single call is trustworthy with probability <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi></mrow><annotation encoding="application/x-tex">p</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span></span></span></span>, the unmonitored trajectory is trustworthy with probability <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>p</mi><mi>n</mi></msup></mrow><annotation encoding="application/x-tex">p^n</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8588em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">p</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.6644em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">n</span></span></span></span></span></span></span></span></span></span></span>. A ninety-five-percent-reliable step becomes a fifty-nine-percent-reliable ten-step trajectory, and a thirteen-percent-reliable forty-step one. Variation in the traditional sense—the spread of a single output around its mean—is no longer the object of interest. The object of interest is the reliability of the <em>trajectory</em>, and it decays geometrically in the absence of intermediate verification.</p>
<p>And therein lies the answer: <strong>the objective is expected loss over the entire trajectory, not variance at any single step</strong>. Quality should be measured by an expected-cost function that weights each error class by the harm it inflicts, by calibration metrics such as the Brier score, which ask whether the model’s stated confidence corresponds to its actual accuracy, and by trajectory-level reliability metrics that account for compounding error across dependent inference steps. A well-calibrated agent that stops and escalates when its confidence is low is often more valuable than a high-confidence one that presses forward through mounting uncertainty.</p>
<h3 id="2-takt-time-equals-available-production-time-divided-by-customer-demand">2. “Takt time equals available production time divided by customer demand”</h3>
<p>Takt time is arguably the single most beloved calculation in the Lean canon, and its logic is impeccable for a manufacturing line whose throughput is bounded by the pace at which physical operators can assemble physical parts. The formula tells the line what rhythm it must maintain to satisfy demand without accumulating inventory or falling behind.</p>
<p>The moment an AI is inserted into a knowledge process, the traditional takt calculation collapses, and it collapses in a way that reveals not one but three latent constraints. On the production side of the value stream—where an AI drafts a memorandum, extracts data from a contract, or summarizes a customer interaction—throughput is bounded not by cycle time but by a combination of three quantities: the rate at which the AI can iterate (particularly for agents, whose loop counts are elastic but not free), the human verification capacity required to police probabilistic output, and the inference budget available to spend on tokens. Any of these three can bind, and which one binds depends on the design of the process.</p>
<p>Applying the old takt formula in this environment leads to nonsensical answers—such as demanding a two-second cycle time from a human reviewer, or overlooking that the model itself has been silently limited by a monthly spend cap—and produces the misleading conclusion that the process is either wildly under-resourced or wildly over-capacity.</p>
<p>Rebuilding the formulation from first principles requires recognizing that, in an AI-augmented process, we must compute the <em>sustainable throughput</em> as the minimum of the constraints imposed by human verification, agent iteration, and token budget:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Sustainable Throughput</mtext><mo>=</mo><mi>min</mi><mo>⁡</mo><mrow><mo fence="true">(</mo><mfrac><mi>H</mi><msub><mi>t</mi><mtext>review</mtext></msub></mfrac><mo separator="true">,</mo><mtext> </mtext><mfrac><mi>L</mi><msub><mi mathvariant="normal">ℓ</mi><mtext>avg</mtext></msub></mfrac><mo separator="true">,</mo><mtext> </mtext><mfrac><mi>B</mi><msub><mi>c</mi><mtext>token</mtext></msub></mfrac><mo fence="true">)</mo></mrow></mrow><annotation encoding="application/x-tex">\text{Sustainable Throughput} = \min\left(\frac{H}{t_{\text{review}}},\ \frac{L}{\ell_{\text{avg}}},\ \frac{B}{c_{\text{token}}}\right)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord text"><span class="mord">Sustainable Throughput</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.8em;vertical-align:-0.65em;"></span><span class="mop">min</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="minner"><span class="mopen delimcenter" style="top:0em;"><span class="delimsizing size2">(</span></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8723em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.334em;"><span style="top:-2.357em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">review</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.143em;"><span></span></span></span></span></span></span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.394em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.0813em;">H</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.4451em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mpunct">,</span><span class="mspace"> </span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8723em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mtight">ℓ</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1645em;"><span style="top:-2.357em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">avg</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2819em;"><span></span></span></span></span></span></span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.394em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">L</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.5423em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mpunct">,</span><span class="mspace"> </span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8723em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathnormal mtight">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3488em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">token</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1512em;"><span></span></span></span></span></span></span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.394em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.0502em;">B</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.4509em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mclose delimcenter" style="top:0em;"><span class="delimsizing size2">)</span></span></span></span></span></span></p>
<p>where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>H</mi></mrow><annotation encoding="application/x-tex">H</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0813em;">H</span></span></span></span> is available human review time, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>t</mi><mtext>review</mtext></msub></mrow><annotation encoding="application/x-tex">t_{\text{review}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7651em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3175em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">review</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> is the expected review duration per output, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>L</mi></mrow><annotation encoding="application/x-tex">L</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal">L</span></span></span></span> is the available agent-loop capacity (bounded by wall-clock latency tolerance and orchestration limits), <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="normal">ℓ</mi><mtext>avg</mtext></msub></mrow><annotation encoding="application/x-tex">\ell_{\text{avg}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9805em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord">ℓ</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">avg</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span></span></span></span> is the average number of loops per completed task, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi></mrow><annotation encoding="application/x-tex">B</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0502em;">B</span></span></span></span> is the token budget for the period, and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>c</mi><mtext>token</mtext></msub></mrow><annotation encoding="application/x-tex">c_{\text{token}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5806em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">token</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> is the token cost of producing one verified output including retries. The human-review term itself must be decomposed to reflect that not every output demands the same scrutiny:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>t</mi><mtext>review</mtext></msub><mo>=</mo><msub><mi>p</mi><mtext>deep</mtext></msub><mo>⋅</mo><msub><mi>t</mi><mtext>deep</mtext></msub><mo>+</mo><msub><mi>p</mi><mtext>light</mtext></msub><mo>⋅</mo><msub><mi>t</mi><mtext>light</mtext></msub></mrow><annotation encoding="application/x-tex">t_{\text{review}} = p_{\text{deep}} \cdot t_{\text{deep}} + p_{\text{light}} \cdot t_{\text{light}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7651em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3175em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">review</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7306em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">p</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">deep</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">⋅</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.9012em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">deep</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7306em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">p</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">light</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">⋅</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.9012em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">light</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>p</mi><mtext>deep</mtext></msub></mrow><annotation encoding="application/x-tex">p_{\text{deep}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7167em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">p</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">deep</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span></span></span></span> is the fraction of outputs requiring substantive adjudication and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>p</mi><mtext>light</mtext></msub></mrow><annotation encoding="application/x-tex">p_{\text{light}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7167em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">p</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">light</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span></span></span></span> is the fraction requiring only a skim.</p>
<p>Let us look at a concrete example to clarify this. Consider an analytics team of five people processing 1,200 customer-inquiry classifications per day. Available review time is <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>5</mn><mo>×</mo><mn>8</mn><mo>×</mo><mn>60</mn><mo>=</mo><mn>2,400</mn></mrow><annotation encoding="application/x-tex">5 \times 8 \times 60 = 2{,}400</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">8</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">60</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord">2</span><span class="mord"><span class="mpunct">,</span></span><span class="mord">400</span></span></span></span> minutes. Traditional takt would demand a two-minute cycle per inquiry, which is barely feasible for a human working alone.</p>
<p>Introduce an AI classifier whose measured trustworthiness on the task is 95 percent, and let deep review consume five minutes while light review consumes 30 seconds. Effective demand on human time becomes:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1,200</mn><mo>×</mo><mo stretchy="false">(</mo><mn>0.05</mn><mo>×</mo><mn>5</mn><mo>+</mo><mn>0.95</mn><mo>×</mo><mn>0.5</mn><mo stretchy="false">)</mo><mo>=</mo><mn>1,200</mn><mo>×</mo><mn>0.725</mn><mo>=</mo><mn>870</mn><mtext> minutes</mtext></mrow><annotation encoding="application/x-tex">1{,}200 \times (0.05 \times 5 + 0.95 \times 0.5) = 1{,}200 \times 0.725 = 870\text{ minutes}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord">1</span><span class="mord"><span class="mpunct">,</span></span><span class="mord">200</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">0.05</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">0.95</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">0.5</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord">1</span><span class="mord"><span class="mpunct">,</span></span><span class="mord">200</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0.725</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6679em;"></span><span class="mord">870</span><span class="mord text"><span class="mord"> minutes</span></span></span></span></span></p>
<p>Utilization has fallen from over 100 percent to roughly 36 percent. But the analysis is not complete: if the classifier is in fact a small agent that calls two retrieval tools and a verifier per inquiry, and each inquiry costs fifteen cents in tokens including retries, then 1,200 inquiries per day cost 180 dollars, and the monthly spend of roughly 4,000 dollars must be reconciled against whatever inference budget the team has been allocated. If the budget is 3,000 dollars, the binding constraint is not the humans but the tokens, and the analysis flips: the leverage points are prompt compression, cheaper-model routing for easy inputs, and caching, rather than headcount.</p>
<p>The strategic question is no longer <em>how do we hire fast enough</em>; it is <em>which of the three constraints is currently binding, and what is the highest-value reallocation of resources across the three</em>, a question the traditional takt calculation never had to confront.</p>
<h3 id="3-aim-for-34-defects-per-million-opportunities">3. “Aim for 3.4 defects per million opportunities”</h3>
<p>The Six Sigma target was a compact way of communicating that six standard deviations should fit between the process mean and the nearest specification limit, allowing for a modest 1.5-sigma drift. It presumes a countable and inspectable notion of a “defect.”</p>
<p>Contemporary AI systems typically operate at error rates measured in whole percentage points—one to five percent on well-scoped tasks, and higher on adversarial or out-of-distribution inputs. That represents 10,000 to 50,000 defects per million opportunities (DPMO), roughly four orders of magnitude worse than the traditional Six Sigma target. Yet many of these systems are demonstrably value-creating because the cost of a defect is not always catastrophic and the cost of production is now negligible.</p>
<p>Furthermore, agentic systems complicate the picture because the relevant defect rate is not that of any individual step but that of the composed trajectory, and the composition is multiplicative. A four-step agent chain whose individual step reliability is ninety-nine percent achieves a trajectory reliability of only ninety-six percent, a fact that Six Sigma’s step-wise intuitions systematically obscure.</p>
<p>The first-principles reformulation is that DPMO must be replaced or supplemented with an <strong>expected-loss framework</strong> that weights defect probability by defect severity, that stratifies error rates by severity class, and that composes multiplicatively along agent trajectories. A 5% stylistic infelicity in a first-draft memorandum is not commensurable with a 5% factual error in a legal filing, and no single scalar DPMO can capture this distinction—much less a scalar DPMO applied to only one link in a ten-step chain.</p>
<h3 id="4-eliminate-the-eight-wastes-timwoods">4. “Eliminate the eight wastes: TIMWOODS”</h3>
<p>Ohno’s taxonomy—Transport, Inventory, Motion, Waiting, Over-processing, Over-production, Defects, and Skills—is not so much wrong as strangely inverted when the producer is an AI. Overproduction, once the cardinal sin because unsold inventory tied up capital and revealed demand mis-forecasts, becomes nearly cost-free at the level of a single draft, though it re-emerges at the scale of an operation as token spend on outputs no one will read.</p>
<p>The waste has migrated: what was once the cost of producing an unneeded widget is now the cost of <em>reviewing</em> and <em>paying to generate</em> an unneeded draft, which consumes the two scarcest resources in the system: human attention and inference budget. Overprocessing, once a matter of adding features the customer did not value, has taken on an AI-native form: an agent that spends forty tool calls polishing an answer the customer would have accepted at ten, burning tokens and latency in pursuit of a marginal quality improvement no one asked for. Waiting, which used to describe operators idling between tasks, now describes AI systems idling while awaiting human adjudication, which is neither wasteful nor eliminable if the humans are the guarantors of quality.</p>
<p>Meanwhile, entirely new categories of waste have emerged and demand recognition. Among them:</p>
<ul>
<li><strong>Hallucination remediation:</strong> The work of detecting and correcting confidently stated falsehoods.</li>
<li><strong>Prompt debt:</strong> The accumulated tax of poorly designed prompts whose brittleness surfaces only under load or after an API update.</li>
<li><strong>Verification overhead:</strong> The human labor absorbed by the need to trust but verify.</li>
<li><strong>Automation bias:</strong> The insidious tendency of reviewers to rubber-stamp AI outputs, which manifests not as visible waste but as latent defect accrual.</li>
<li><strong>Model drift:</strong> The silent degradation of a deployed model as the world it was trained on recedes into the past.</li>
<li><strong>Guardrail brittleness:</strong> The maintenance burden of the ever-lengthening list of exceptions and safety filters that surround a production model.</li>
<li><strong>Context-window thrash:</strong> The inefficiency of repeatedly stuffing and re-stuffing an AI with the same background material.</li>
<li><strong>Sycophancy loss:</strong> The subtle failure of systems trained to please, which agree with erroneous premises and thereby corrupt downstream decisions.</li>
<li><strong>Loop thrash:</strong> The agentic waste of iterations that revisit the same reasoning, oscillate between candidate solutions, or pursue instrumentally reasonable but strategically misaligned subgoals until a budget forces termination.</li>
<li><strong>Tool-call sprawl:</strong> The tendency of agents to invoke more tools than the task requires, each call adding latency, tokens, and new failure surface.</li>
<li><strong>Verifier tax:</strong> The token and latency cost of self-critique passes, self-consistency ensembles, and second-model verifiers, which improve quality but at a cost that must be tallied.</li>
<li><strong>Cache neglect:</strong> The failure to memoize deterministic sub-computations, causing the same reasoning to be paid for repeatedly.</li>
</ul>
<p>A rigorous Lean practitioner in an AI-augmented environment must inventory these new wastes with the same discipline that Ohno once brought to the shop floor. Several of them—loop thrash, tool-call sprawl, cache neglect—are direct analogs of the industrial wastes Ohno cataloged, transposed from the movement of physical goods to the movement of tokens through a computational value stream. The isomorphism is worth savoring: even the most exotic AI-native inefficiencies turn out, on inspection, to be <em>motion</em>, <em>transport</em>, and <em>inventory</em> wearing new clothes.</p>
<h3 id="5-ask-why-five-times">5. “Ask why five times”</h3>
<p>Root-cause analysis in classical Six Sigma presumed that the causal chain was legible to a diligent investigator. When a lathe produces a scratched surface, one can trace the scratch to the tool, the tool to the operator, the operator to the training, and so on. Neural networks are not legible in this mechanical sense. The proximate cause of a particular hallucination may be a subtle, high-dimensional interaction among billions of parameters, none of which is individually accountable for the output. The five whys, applied naively at the token level, will terminate in unsatisfying tautologies: “because the model was trained on data that biased it in this direction.”</p>
<p>The problem sharpens when the failing artifact is not a single output but an agent trajectory. Consider an agent that produced the wrong answer after twenty-three tool calls: which call is the root cause? The proximate error may have been introduced at step nineteen, but the misconception that led to step nineteen may have been seeded at step six, where an ambiguous retrieval result was interpreted with unwarranted confidence. Root-cause analysis on agent trajectories therefore requires <em>trajectory replay</em>, in which the sequence of states, tool outputs, and reasoning steps is reconstructed and inspected for the earliest divergence from a well-behaved path.</p>
<p>The reformulation is that the object of root-cause analysis must shift up the stack. One does not ask why the model produced a specific token; one asks why the <em>evaluation harness</em> failed to catch the error class, why the <em>training data</em> contained the misleading pattern, why the <em>deployment context</em> fell outside the validated distribution, why the <em>agent’s planning policy</em> pursued a costly subgoal, or why the <em>loop budget</em> was set high enough to permit the divergence to compound. The whys are simply asked of the system architecture rather than the individual inference.</p>
<h3 id="6-standardize-work-before-you-improve-it">6. “Standardize work before you improve it”</h3>
<p>Standardization was the pre-condition for continuous improvement because without a stable baseline, one could not tell whether a change had helped. In an AI-augmented process, the “work” performed for any two customers may be genuinely different, dynamically re-planned by an agent, and personalized to a degree that would have been impossible for a human operator to sustain.</p>
<p>The standard cannot be the <em>object-level procedure</em>; it must be the <em>meta-procedure</em>—the prompt template, the evaluation harness, the escalation policy, the guardrail configuration, and, in the agentic case, the tool inventory, the planning policy, the loop budget, and the termination criteria. Improvement in the AI age is often the improvement of the process that generates the process, a level of abstraction that traditional standard-work documents were never designed to capture.</p>
<h3 id="7-poka-yoke-prevents-mistakes-at-the-source">7. “Poka-yoke prevents mistakes at the source”</h3>
<p>Mistake-proofing worked because human errors and mechanical failures tend to have discrete, enumerable modes. A jig that only accepts a part in the correct orientation eliminates an entire failure class. AI failure modes are neither discrete nor fully enumerable because the input space is combinatorial and adversarial inputs can be constructed to defeat any static guardrail.</p>
<p>The reformulated principle is that poka-yoke in an AI process must itself be probabilistic and adaptive: guard classifiers, uncertainty thresholds, red-team-generated test suites, and human-in-the-loop escalation for low-confidence outputs.</p>
<p>Agentic systems demand an additional class of poka-yoke that has no clean pre-AI analog: the <em>self-imposed loop budget</em>. An agent that has been granted the authority to iterate must also be granted a rigorously enforced ceiling on iterations, tool calls, and tokens beyond which it must escalate rather than persist. This is the AI-native analog of Toyota’s <em>jidoka</em>—the authority to stop the line—now exercised by the process upon itself. Without it, an agent’s failure mode is not merely to be wrong but to be <em>expensively</em> wrong, having consumed hours of latency and thousands of tokens in pursuit of a completion it was never going to reach. The design problem is no longer merely to prevent a known failure but to detect an unknown one <em>and</em> to bound the cost of the detection process.</p>
<h3 id="8-kaizen-small-continuous-improvements">8. “Kaizen: small, continuous improvements”</h3>
<p>Kaizen assumed a monotonically improving landscape where each small change made things a little better, and the changes composed. AI systems do not always cooperate with this assumption. A minor prompt edit can produce a non-linear shift in behavior across the input distribution. A model upgrade can improve average performance while regressing on a critical subpopulation. A new tool added to an agent’s inventory can be helpful in isolation but disastrous in combination with an existing tool whose behavior it subtly reinterprets. Improvements do not always compose because the model’s behavior on task A is entangled, through shared parameters and shared context, with its behavior on tasks B and C.</p>
<p>The reformulation is that kaizen must be <em>evaluation-driven</em> rather than intuition-driven, and that the evaluation harness itself becomes the improvement’s most important artifact. For agentic systems the harness must judge <em>trajectories</em> rather than <em>outputs</em>, scoring not merely whether the final answer was correct but whether the intermediate steps were parsimonious, honest about uncertainty, appropriately deferential at high-stakes junctures, and economical in their use of tokens and tool calls. Every change must be regression-tested against a stable, adversarially constructed benchmark, or the composition property will silently fail.</p>
<hr>
<h2 id="part-iii-mapping-the-shift-in-practices">Part III: Mapping the Shift in Practices</h2>
<p>Having run through these expirations, we can compile how these changes map onto the daily realities of process improvement. In classical Lean Six Sigma, the focus was on deterministic variables and minimizing physical variance. In the AI-augmented value stream, we manage probabilistic behaviors and optimize lifecycle economics.</p>
<table>
<thead>
<tr>
<th align="left">Operational Dimension</th>
<th align="left">Classical Lean Six Sigma Practice</th>
<th align="left">AI-Augmented Cognitive Practice</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><strong>Primary Target</strong></td>
<td align="left">Process Variance (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>σ</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">\sigma^2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0359em;">σ</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span></span></span></span>)</td>
<td align="left">Expected Loss (Weighted by Severity)</td>
</tr>
<tr>
<td align="left"><strong>Quality Validation</strong></td>
<td align="left">Tolerance limits (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>C</mi><mi>p</mi></msub><mo>≥</mo><mn>1.33</mn></mrow><annotation encoding="application/x-tex">C_p \ge 1.33</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9694em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0715em;">C</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0715em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">p</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">≥</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">1.33</span></span></span></span>)</td>
<td align="left">Calibration &#x26; Reliability (Brier Score)</td>
</tr>
<tr>
<td align="left"><strong>Pace Setter</strong></td>
<td align="left">Physical Production Takt</td>
<td align="left">Human Verification Takt</td>
</tr>
<tr>
<td align="left"><strong>Process Capacity</strong></td>
<td align="left">Machine/Operator Cycle Rate</td>
<td align="left">Sustainable Throughput (constrained by token budgets &#x26; loops)</td>
</tr>
<tr>
<td align="left"><strong>Defect Metric</strong></td>
<td align="left">Scalar DPMO (target: 3.4)</td>
<td align="left">Expected Loss Framework &#x26; Multiplicative Trajectory Reliability</td>
</tr>
<tr>
<td align="left"><strong>Waste Elimination</strong></td>
<td align="left">TIMWOODS taxonomy</td>
<td align="left">Cognitive Wastes (Loop thrash, prompt debt, verifier tax)</td>
</tr>
<tr>
<td align="left"><strong>Continuous Improvement</strong></td>
<td align="left">Kaizen &#x26; Object-Level Standardization</td>
<td align="left">Evaluation Harness &#x26; Meta-Standardization</td>
</tr>
<tr>
<td align="left"><strong>Error Prevention</strong></td>
<td align="left">Physical Poka-Yoke (mechanical jigs)</td>
<td align="left">Adaptive Poka-Yoke (classifiers &#x26; self-imposed loop budgets)</td>
</tr>
<tr>
<td align="left"><strong>Root Cause Analysis</strong></td>
<td align="left">The Five Whys (linear causal chains)</td>
<td align="left">Trajectory Replays &#x26; Architectural Whys</td>
</tr>
<tr>
<td align="left"><strong>Lifecycle Control</strong></td>
<td align="left">Static Control Charts (SPC)</td>
<td align="left">Continuous Monitoring, Drift Detection, and Retraining Loops</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="part-iv-juran-reborn--the-cost-of-quality-in-the-token-economy">Part IV: Juran Reborn — The Cost of Quality in the Token Economy</h2>
<p>Of all the classical Lean Six Sigma frameworks, Juran’s cost-of-quality decomposition survives the transition to AI in the most nearly intact form, and it deserves separate treatment because it is the framework most likely to be misapplied in the near term. Juran taught that total quality cost decomposes into four categories: prevention, appraisal, internal failure, and external failure. The optimum sits where the sum of the four is minimized.</p>
<p>The four buckets refill with almost eerie precision in an AI-augmented operation:</p>
<ul>
<li><strong>Prevention:</strong> Now includes prompt engineering, fine-tuning, retrieval-augmented grounding, careful tool selection for agents, and the design of loop budgets and termination criteria.</li>
<li><strong>Appraisal:</strong> Now includes verifier tokens, self-critique passes, self-consistency ensembles, guard-classifier calls, and the labor of human reviewers.</li>
<li><strong>Internal Failure:</strong> Now includes agent loops that must be aborted, retried outputs, rejected drafts, and the token cost of every reasoning trace that did not survive verification.</li>
<li><strong>External Failure:</strong> Now includes the reputational, legal, and customer-satisfaction cost of a hallucination that reached the customer, and it is by far the largest of the four when it materializes.</li>
</ul>
<p>The strategic point is that these four categories are substitutes for one another across a curve whose shape must be estimated empirically.</p>
<p>Under-investing in prevention forces higher appraisal spend; under-investing in both makes internal failure explode and eventually leaks into external failure. Over-investing in appraisal—say, by running three verifier passes on every output—reduces external failure to nearly zero but at a per-output cost that may exceed the value of the output itself. The cost-per-verified-output metric is precisely the operational proxy for finding the Juran minimum in the token economy: it measures the total spend across prevention, appraisal, and internal failure per unit of useful yield, and it can be tracked over time as the model, the harness, and the process mature.</p>
<p>A recalibrated cost-of-quality analysis therefore becomes the CFO-facing lingua franca for AI operations. It makes visible a trade-off that is often invisible in ad hoc AI deployments: that money saved by skimping on prevention and appraisal reliably reappears, magnified, in external failure.</p>
<hr>
<h2 id="part-v-a-recalculated-littles-law-and-its-strategic-implication">Part V: A Recalculated Little’s Law and Its Strategic Implication</h2>
<p>Little’s Law—that work in process (WIP) equals throughput multiplied by cycle time—is a mathematical identity and therefore survives the transition to AI intact.</p>
<p>Its <em>strategic</em> interpretation, however, is transformed. In the pre-AI regime, throughput was bounded by headcount, and Little’s Law implied that reducing WIP required either hiring or reducing cycle time. In the AI-augmented regime, throughput on the production side is elastic within limits set by inference budget and agent-loop capacity, which means WIP can accumulate explosively unless the <em>verification</em> side of the process can absorb it.</p>
<p>An LLM can draft ten thousand contracts or customer communications in minutes, creating massive WIP queues that overwhelm downstream teams. The strategic lesson is that leaders must now plan verification capacity, iteration capacity, and token capacity as deliberately as their predecessors planned production capacity. The traditional business intuition—“we need more headcount to produce more output”—has been inverted. We now need headcount, differently trained and differently deployed, to <em>govern</em> what the machines produce.</p>
<p>The bottleneck has not disappeared; it has multiplied and migrated. Where a factory once had one binding constraint at a time and could be managed by identifying and elevating it in the tradition of Goldratt, an AI-augmented operation typically has three latent constraints—human review time, loop limits, and token budgets—and toggles among them as conditions change. This is the single most important operational fact of the coming decade.</p>
<hr>
<h2 id="part-vi-what-endures">Part VI: What Endures</h2>
<p>Nothing in the preceding argument suggests that Lean Six Sigma has been superseded. The first principles—that quality is economic, that variation destroys predictability, that waste is what the customer would not pay for, that improvement is empirical—remain not merely valid but indispensable. Indeed, Juran’s cost-of-quality decomposition, Ohno’s taxonomy of wastes, and Goldratt’s theory of constraints emerge from the AI transition with their conceptual architectures intact and their vocabularies enriched rather than obsolete. What has changed is the derivation layer, the accumulated body of intermediate heuristics that once translated the first principles into daily practice.</p>
<p>The intellectually honest response to this shift is neither to abandon Lean Six Sigma nor to genuflect before its inherited vocabulary. It is to return to the primary sources—Shewhart, Deming, Juran, Ohno, Ishikawa, Goldratt—read them with the awareness that their examples were drawn from the world they knew, and reconstruct the derivation for the world we now inhabit.</p>
<p>In our <a href="/blog/2026/07/28/the-most-expensive-question-in-ml.html">earlier analysis on scaling laws</a>, we saw how training-time and serving-time economics force us to balance resources between parameter counts and dataset tokens. In our <a href="/blog/2026/07/23/the-recursion-turns-on-itself-autoresearch.html">analysis of recursive loops</a>, we saw how nested workflows optimize execution paths. In this paper, we see where these computational trade-offs meet the corporate value stream.</p>
<p>The question, in the end, is not whether the bell curve still describes the world. It is whether we have the intellectual courage to notice that it has quietly bent, that the process it once described has become three processes running in parallel and constrained by three different scarcities, and to redraw our maps accordingly.</p>
<p><em>Victor Blancada is a data scientist focused on deriving actionable insights for clients. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="data science" /><category term="lean six sigma" /><category term="artificial intelligence" /><category term="optimization" /><category term="statistics" /><summary type="html">Lean Six Sigma was forged in late-twentieth-century manufacturing. Here is how we must re-derive its heuristics for the age of machine intelligence.</summary></entry><entry><title type="html">The Most Expensive Question in Machine Learning: Scaling Laws and the Economics of Scale</title><link href="https://victorangeloblancada.github.io/blog/2026/07/28/the-most-expensive-question-in-ml.html" rel="alternate" type="text/html" title="The Most Expensive Question in Machine Learning: Scaling Laws and the Economics of Scale" /><published>2026-07-28T00:00:00.000Z</published><updated>2026-07-28T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/07/28/the-most-expensive-question-in-ml</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/07/28/the-most-expensive-question-in-ml.html"><![CDATA[<p>The economist Thomas Sowell famously remarked that “there are no solutions, only trade-offs.” In no field is this axiom more painfully (and expensively) visible today than in the engineering of frontier artificial intelligence. Before a laboratory turns on its GPU clusters for a multi-million-dollar training run, it must answer what is arguably the most expensive question in computer science: given a fixed budget of compute, should the capital buy a larger model, or should it buy more data to train a smaller one?</p>
<p>An incorrect answer costs tens of millions of dollars and consigns a model to irrelevance. The correct answer has changed twice in the last six years, and understanding why is the closest thing the field has to a general theory of its own economics.</p>
<hr>
<h2 id="the-kaplan-recipe-parameters-over-data">The Kaplan Recipe: Parameters Over Data</h2>
<p>The first attempt at a general theory of scaling came in January 2020, when Jared Kaplan and his colleagues at OpenAI published <em>“Scaling Laws for Neural Language Models.”</em> Sweeping across seven orders of magnitude of compute, they found that a model’s cross-entropy loss followed clean power laws. The architectural details (width, depth, number of heads) were largely irrelevant within a broad range. The critical factors were parameters <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi></mrow><annotation encoding="application/x-tex">N</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.109em;">N</span></span></span></span>, dataset size <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>D</mi></mrow><annotation encoding="application/x-tex">D</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0278em;">D</span></span></span></span>, and total compute <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>C</mi></mrow><annotation encoding="application/x-tex">C</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0715em;">C</span></span></span></span>.</p>
<p>Kaplan’s most consequential conclusion was operational. For a given compute budget, the optimal allocation of resources favored model size over data volume:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>N</mi><mtext>optimal</mtext></msub><mo>∝</mo><msup><mi>C</mi><mn>0.73</mn></msup><mo separator="true">,</mo><mspace width="2em"></mspace><msub><mi>D</mi><mtext>optimal</mtext></msub><mo>∝</mo><msup><mi>C</mi><mn>0.27</mn></msup></mrow><annotation encoding="application/x-tex">N_{\text{optimal}} \propto C^{0.73}, \qquad D_{\text{optimal}} \propto C^{0.27}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9694em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.109em;">N</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.109em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">optimal</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∝</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.1002em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0715em;">C</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">0.73</span></span></span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:2em;"></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0278em;">D</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">optimal</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∝</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8141em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0715em;">C</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">0.27</span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>This relationship translated into a simple industry slogan: larger models are radically more sample-efficient. The operational advice was to buy parameters and stop training well short of convergence. The industry followed this advice. GPT-3 (175B parameters) was trained on roughly 300 billion tokens—a ratio of only 1.7 tokens per parameter. Gopher (280B) and Megatron-Turing NLG (530B) followed the same recipe.</p>
<hr>
<h2 id="the-chinchilla-correction-data-was-under-ordered">The Chinchilla Correction: Data Was Under-Ordered</h2>
<p>In March 2022, Jordan Hoffmann and the DeepMind team challenged this orthodoxy. In their paper, <em>“Training Compute-Optimal Large Language Models,”</em> they held compute budgets constant and swept the ratio of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi></mrow><annotation encoding="application/x-tex">N</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.109em;">N</span></span></span></span> to <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>D</mi></mrow><annotation encoding="application/x-tex">D</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0278em;">D</span></span></span></span> using the “IsoFLOP” methodology. Their U-shaped performance curves showed that model size and dataset size should scale in equal proportion:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>N</mi><mtext>optimal</mtext></msub><mo>∝</mo><msup><mi>C</mi><mn>0.50</mn></msup><mo separator="true">,</mo><mspace width="2em"></mspace><msub><mi>D</mi><mtext>optimal</mtext></msub><mo>∝</mo><msup><mi>C</mi><mn>0.50</mn></msup></mrow><annotation encoding="application/x-tex">N_{\text{optimal}} \propto C^{0.50}, \qquad D_{\text{optimal}} \propto C^{0.50}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9694em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.109em;">N</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.109em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">optimal</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∝</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.1002em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0715em;">C</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">0.50</span></span></span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:2em;"></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0278em;">D</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">optimal</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∝</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8141em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0715em;">C</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">0.50</span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>This yielded the now-canonical rule of thumb: a compute-optimal model requires roughly 20 tokens per parameter. To prove their thesis, DeepMind trained Chinchilla, a 70B parameter model, on 1.4 trillion tokens using the same compute budget as the 280B Gopher. Chinchilla outperformed Gopher, GPT-3, and other larger models across the board. The implication was clear: the industry had spent billions of dollars training models that were, in Hoffmann’s polite formulation, “significantly undertrained.”</p>
<p>Funnily enough, the technical discrepancy between Kaplan and Chinchilla was not resolved by algorithmic breakthroughs, but by a simple correction in counting. In June 2024, Tim Pearce and Jinyeop Song demonstrated that Kaplan’s steeper scaling exponent was a counting artifact. Kaplan had measured <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi></mrow><annotation encoding="application/x-tex">N</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.109em;">N</span></span></span></span> using only non-embedding parameters. At small training runs, embedding tables consume a large fraction of the total parameter count, distorting the slope. Once the embedding parameters are accounted for, both approaches converge on the Chinchilla optimal ratio.</p>
<hr>
<h2 id="interactive-scaling-law-calculator">Interactive Scaling Law Calculator</h2>
<p>To visualize how these scaling regimes allocate compute, and how inference volume shifts the optimal trade-off, use the interactive calculator below. Adjust the training compute budget and the expected query volume to see how the optimal model size and token count adapt.</p>
<div style="background: rgba(30, 41, 59, 0.75); border: 1px solid rgba(255, 255, 255, 0.1); border-radius: 12px; padding: 24px; color: #f8fafc; font-family: -apple-system, BlinkMacSystemFont, &#x27;Segoe UI&#x27;, Roboto, sans-serif; margin: 24px 0; backdrop-filter: blur(8px);">
  <h3 style="margin-top: 0; color: #38bdf8; font-size: 1.25rem; border-bottom: 1px solid rgba(255, 255, 255, 0.1); padding-bottom: 8px;">Lifecycle Compute Optimizer</h3>
  <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px;">
    <div>
      <label style="display: block; font-size: 0.85rem; color: #94a3b8; margin-bottom: 6px;">Training Compute Budget (log FLOPs)</label>
      <input type="range" id="computeSlider" aria-label="Training compute budget (log FLOPs)" min="21" max="26" step="0.1" value="23.8" style="width: 100%; accent-color: #38bdf8;">
      <div id="computeVal" style="font-size: 0.9rem; font-weight: bold; margin-top: 4px; color: #e2e8f0;">10^23.8 FLOPs (Llama 3 8B class)</div>
    </div>
    <div>
      <label style="display: block; font-size: 0.85rem; color: #94a3b8; margin-bottom: 6px;">Lifetime Inference Queries (Billions)</label>
      <input type="range" id="querySlider" aria-label="Lifetime inference queries (billions)" min="0" max="1000" step="10" value="100" style="width: 100%; accent-color: #38bdf8;">
      <div id="queryVal" style="font-size: 0.9rem; font-weight: bold; margin-top: 4px; color: #e2e8f0;">100 Billion queries</div>
    </div>
  </div>
  <table style="width: 100%; border-collapse: collapse; text-align: left; font-size: 0.9rem;">
    <thead>
      <tr style="border-bottom: 1px solid rgba(255, 255, 255, 0.2); color: #38bdf8;">
        <th style="padding: 8px;">Scaling Regime</th>
        <th style="padding: 8px;">Model Size (N)</th>
        <th style="padding: 8px;">Dataset Size (D)</th>
        <th style="padding: 8px;">Tokens/Param Ratio</th>
      </tr>
    </thead>
    <tbody>
      <tr style="border-bottom: 1px solid rgba(255, 255, 255, 0.1);">
        <td style="padding: 8px; font-weight: bold; color: #f43f5e;">Kaplan (2020)</td>
        <td style="padding: 8px;" id="kaplanN">--</td>
        <td style="padding: 8px;" id="kaplanD">--</td>
        <td style="padding: 8px;" id="kaplanRatio">--</td>
      </tr>
      <tr style="border-bottom: 1px solid rgba(255, 255, 255, 0.1);">
        <td style="padding: 8px; font-weight: bold; color: #10b981;">Chinchilla (2022)</td>
        <td style="padding: 8px;" id="chinchillaN">--</td>
        <td style="padding: 8px;" id="chinchillaD">--</td>
        <td style="padding: 8px;" id="chinchillaRatio">--</td>
      </tr>
      <tr style="border-bottom: 1px solid rgba(255, 255, 255, 0.1); background: rgba(56, 189, 248, 0.05);">
        <td style="padding: 8px; font-weight: bold; color: #38bdf8;">Amortized Inference</td>
        <td style="padding: 8px;" id="amortizedN">--</td>
        <td style="padding: 8px;" id="amortizedD">--</td>
        <td style="padding: 8px;" id="amortizedRatio">--</td>
      </tr>
    </tbody>
  </table>
  <div style="font-size: 0.75rem; color: #64748b; margin-top: 12px; line-height: 1.3;">
    *Calculations assume 6 FLOPs/parameter per token during training and 2 FLOPs/parameter per token (500 tokens/query) during inference.
  </div>
</div>
<script>
(function() {
  const computeSlider = document.getElementById('computeSlider');
  const querySlider = document.getElementById('querySlider');
  const computeVal = document.getElementById('computeVal');
  const queryVal = document.getElementById('queryVal');

  const k_capN = document.getElementById('kaplanN');
  const k_capD = document.getElementById('kaplanD');
  const k_capR = document.getElementById('kaplanRatio');

  const c_chinN = document.getElementById('chinchillaN');
  const c_chinD = document.getElementById('chinchillaD');
  const c_chinR = document.getElementById('chinchillaRatio');

  const a_amortN = document.getElementById('amortizedN');
  const a_amortD = document.getElementById('amortizedD');
  const a_amortR = document.getElementById('amortizedRatio');

  function update() {
    const logC = parseFloat(computeSlider.value);
    const C = Math.pow(10, logC);
    const Q_queries = parseFloat(querySlider.value) * 1e9;
    const Q_tokens = Q_queries * 500; // 500 tokens generated per query

    // Label mapping for visual clarity
    let classLabel = "Custom";
    if (logC >= 25.5) classLabel = "GPT-4 class";
    else if (logC >= 24.8) classLabel = "Llama 3 70B class";
    else if (logC >= 23.5) classLabel = "Llama 3 8B class";
    else if (logC >= 22.8) classLabel = "GPT-3 class";
    else classLabel = "Small Prototype";

    computeVal.innerText = `10^${logC.toFixed(1)} FLOPs (${classLabel})`;
    queryVal.innerText = `${querySlider.value} Billion queries`;

    // 1. Kaplan Allocation
    // Calibrated so that C = 5.04e23 FLOPs gives N = 280B, D = 300B
    const kaplanRefC = 5.04e23;
    const N_kaplan = 280e9 * Math.pow(C / kaplanRefC, 0.73);
    const D_kaplan = 300e9 * Math.pow(C / kaplanRefC, 0.27);

    // 2. Chinchilla Allocation
    // Calibrated so that C = 5.88e23 FLOPs gives N = 70B, D = 1400B
    const chinchillaRefC = 5.88e23;
    const N_chinchilla = 70e9 * Math.sqrt(C / chinchillaRefC);
    const D_chinchilla = 1400e9 * Math.sqrt(C / chinchillaRefC);

    // 3. Amortized Inference Allocation
    // Solves the resource allocation shifting under inference cost.
    // Total lifecycle budget C_total = C_train + C_infer.
    // We hold Training Compute fixed at C = 6ND.
    // We find N that minimizes total loss including inference burden.
    // Fixed point solver: N_new = N_chinchilla * (1 + Q / 3D)^(-0.28)
    let N_amort = N_chinchilla;
    let D_amort = D_chinchilla;
    if (Q_tokens > 0) {
      let low_N = 1e6;
      let high_N = C / 6;
      for (let iter = 0; iter < 40; iter++) {
        N_amort = (low_N + high_N) / 2;
        D_amort = C / (6 * N_amort);
        let lhs = 138.176 * Math.pow(N_amort, -0.34);
        let rhs = 114.996 * Math.pow(D_amort, -0.28) * (1 + Q_tokens / (3 * D_amort));
        if (lhs > rhs) {
          low_N = N_amort;
        } else {
          high_N = N_amort;
        }
      }
    }

    // Formatting helper
    function formatParams(num) {
      if (num >= 1e12) return (num / 1e12).toFixed(2) + ' T';
      if (num >= 1e9) return (num / 1e9).toFixed(1) + ' B';
      return (num / 1e6).toFixed(1) + ' M';
    }

    function formatTokens(num) {
      if (num >= 1e12) return (num / 1e12).toFixed(2) + ' T';
      if (num >= 1e9) return (num / 1e9).toFixed(1) + ' B';
      return (num / 1e6).toFixed(1) + ' M';
    }

    k_capN.innerText = formatParams(N_kaplan);
    k_capD.innerText = formatTokens(D_kaplan);
    k_capR.innerText = (D_kaplan / N_kaplan).toFixed(1) + ' : 1';

    c_chinN.innerText = formatParams(N_chinchilla);
    c_chinD.innerText = formatTokens(D_chinchilla);
    c_chinR.innerText = (D_chinchilla / N_chinchilla).toFixed(1) + ' : 1';

    a_amortN.innerText = formatParams(N_amort);
    a_amortD.innerText = formatTokens(D_amort);
    a_amortR.innerText = (D_amort / N_amort).toFixed(1) + ' : 1';
  }

  computeSlider.addEventListener('input', update);
  querySlider.addEventListener('input', update);
  update();
})();
</script>
<hr>
<h2 id="the-amortized-inference-shift">The Amortized Inference Shift</h2>
<p>Yet, if you look at the open-weights landscape today, almost nobody obeys the Chinchilla-optimal 20:1 ratio.</p>
<ul>
<li><strong>Meta’s Llama 3 8B</strong> was trained on over 15 trillion tokens—a ratio of 1,875 tokens per parameter.</li>
<li><strong>Alibaba’s Qwen 2.5 7B</strong> saw 18 trillion tokens—a ratio of over 2,570 tokens per parameter.</li>
<li><strong>DeepSeek V3</strong> pretrained on 14.8 trillion tokens, representing roughly 400 tokens per active parameter.</li>
</ul>
<p>This deliberate “overtraining” does not contradict Chinchilla. Rather, it optimizes for a different objective function. Hoffmann’s equations minimized the compute required to <em>train</em> a model. They ignored the cost of running that model in production after training ends.</p>
<p>Once a model is deployed to millions of users, the lifetime cost of serving inference dwarfs the upfront cost of training. A smaller, overtrained model requires fewer GPUs to run, saving millions in serving infrastructure (which is why our own supply chain algorithms are constantly being pruned for execution speed). Overtraining is training-compute suboptimal, but it is highly compute-rational over the model’s operational lifespan.</p>
<p>A recent Wisconsin–Stanford paper, <em>Test-Time Scaling Makes Overtraining Compute-Optimal</em>, has formalized this by adding a third axis—inference-time samples—and finds that when reasoning models like o1 or DeepSeek-R1 will be sampled hundreds of times per query, the optimum shifts radically into the overtrained regime, beyond the range most pretraining studies even measure. In our <a href="/blog/2026/07/23/the-recursion-turns-on-itself-autoresearch.html">analysis of recursive AutoResearch loops</a>, we saw how stacking reasoning loops at inference time routes around model constraints, reinforcing the value of highly optimized base models.</p>
<hr>
<h2 id="the-densing-law-and-capability-density">The Densing Law and Capability Density</h2>
<p>This structural efficiency is validated by what researchers call <strong>capability density</strong>—the ratio of the “effective parameter size” (how large an older model would need to be to match a modern model’s benchmarks) to the actual parameter size.</p>
<p>In late 2024, a team from Tsinghua and OpenBMB introduced the <strong>Densing Law</strong>, documenting that the capability density of open-weights models has doubled roughly every 3.5 months.</p>
<table>
<thead>
<tr>
<th align="center">Model</th>
<th align="center">Release Date</th>
<th align="center">Parameter Count</th>
<th align="center">MMLU Score</th>
<th align="center">Relative Capability Density</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><strong>LLaMA 1</strong></td>
<td align="center">Feb 2023</td>
<td align="center">65B</td>
<td align="center">68.9%</td>
<td align="center">1.0x (Baseline)</td>
</tr>
<tr>
<td align="center"><strong>Qwen 2.5</strong></td>
<td align="center">Sep 2024</td>
<td align="center">72B</td>
<td align="center">85.3%</td>
<td align="center">4.8x</td>
</tr>
<tr>
<td align="center"><strong>Llama 3.1</strong></td>
<td align="center">Jul 2024</td>
<td align="center">70B</td>
<td align="center">86.0%</td>
<td align="center">5.1x</td>
</tr>
</tbody>
</table>
<p>This exponential increase explains why a modern 72B model can match or exceed a 405B model trained just a year prior. It is not luck; it is the compounding impact of superior data curation, better tokenizers, and structural architectural refinements like Grouped-Query Attention (GQA) and SwiGLU activations.</p>
<hr>
<h2 id="the-shrinking-fine-tuning-headroom">The Shrinking Fine-Tuning Headroom</h2>
<p>This density shift has practical implications for enterprise adaptation. Base models today arrive already saturated with post-training. Qwen 2.5 underwent supervised fine-tuning on over a million instruction samples, followed by multi-stage reinforcement learning with both offline DPO and online GRPO.</p>
<p>Because the base model has already had its low-hanging fruit harvested, generic instruction fine-tuning now yields diminishing marginal returns. Downstream teams should focus their efforts away from general fluency and toward domain-specific adaptation (e.g., specialized legal reasoning, medical coding, or unique brand voices) where the general-purpose pretraining distribution remains sparse. Fortunately, parameter-efficient fine-tuning (PEFT) methods like LoRA have reduced these costs to the point where the business question is no longer whether to fine-tune, but what domain data to prioritize.</p>
<p>In our <a href="/blog/2026/07/31/lean-six-sigma-ai.html">upcoming analysis of Lean Six Sigma in AI</a>, we will explore how these overtrained models redefine the cost of quality in the corporate value stream.</p>
<p>Ultimately, scaling laws are not immutable laws of physics. They are contingent, empirical, and sensitive to what we count, how we measure it, and how we plan to deploy the system. The labs winning the market today are not those with the largest compute clusters, but those who read the trajectory of these trade-offs the fastest.</p>
<p><em>Victor Blancada is a data scientist focused on deriving actionable insights for clients. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="data science" /><category term="machine learning" /><category term="business strategy" /><category term="economics" /><summary type="html">The optimal allocation of compute between model parameters and dataset size has changed twice in six years. Modern inference economics and the Densing Law are redrawing the map of AI competitiveness.</summary></entry><entry><title type="html">The Recursion Turns on Itself: Karpathy's AutoResearch and Bilevel Meta-Loops</title><link href="https://victorangeloblancada.github.io/blog/2026/07/23/the-recursion-turns-on-itself-autoresearch.html" rel="alternate" type="text/html" title="The Recursion Turns on Itself: Karpathy's AutoResearch and Bilevel Meta-Loops" /><published>2026-07-23T00:00:00.000Z</published><updated>2026-07-23T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/07/23/the-recursion-turns-on-itself-autoresearch</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/07/23/the-recursion-turns-on-itself-autoresearch.html"><![CDATA[<p>In <em>Gödel, Escher, Bach</em>, Douglas Hofstadter famously explored the concept of “strange loops”—phenomena in which, by moving upwards through a hierarchical system, one unexpectedly finds themselves back at the starting point. It is a concept that has fascinated logicians, mathematicians, and artists for decades because it suggests that self-reference can generate meaning out of purely mechanical rules. Yet, in computer science, our self-improving loops have historically been more of a conceptual joke than a practical reality.</p>
<p>There is a peculiar joke buried inside modern machine learning. The field’s most celebrated activity—scientific research—is, viewed sideways, one of the most mechanical things humans do. A researcher forms a hypothesis, edits code, runs an experiment, reads the metric, keeps or discards the change, and repeats the cycle. A good researcher gets through maybe ten of these iterations on a productive day, most of it spent waiting for a GPU. The obvious question, once large language models became reliable code editors, was why a human should be inside that loop at all. Andrej Karpathy’s answer, delivered in March 2026 as roughly 630 lines of Python, was simple: they shouldn’t be.</p>
<p>Karpathy’s repository, <em>AutoResearch</em>, is small on purpose. Three files matter. The file <code>prepare.py</code> handles data preparation and the evaluation harness, and the agent is forbidden from touching it. The file <code>train.py</code>—a single-file GPT trainer built on Muon and AdamW, derived from Karpathy’s nanochat—is fair game; the agent can modify the architecture, optimizer, batch size, and learning rate. The file <code>program.md</code> is the human-authored English brief that tells the agent what to explore and what constraints to respect. The loop itself is embarrassingly simple: propose a change to <code>train.py</code>, train for a fixed five-minute wall-clock budget, measure validation bits-per-byte, keep if better, revert if not, and repeat overnight.</p>
<hr>
<h2 id="the-ratchet-and-the-bilevel-formulation">The Ratchet and the Bilevel Formulation</h2>
<p>Karpathy calls it a “ratchet loop,” and the framing matters. This is emphatically not standard hyperparameter search. Tools like Optuna and Ray Tune walk a predefined grid; AutoResearch lets the agent modify arbitrary code, meaning the search space is whatever the language model can construct. Nor is it quite Google’s AlphaEvolve, which is closed and evolutionary. AutoResearch is a bet that a general-purpose model’s priors—its taste, absorbed from the entire machine learning literature during pretraining—make it a better proposer than any random search algorithm.</p>
<p>The empirical results were, for a 630-line repository, unreasonably good. Pointed at his own already-tuned nanochat, Karpathy’s agent ran ~700 experiments over two days and found ~20 genuine improvements, cutting time-to-GPT-2-quality by 11 percent. Shopify’s Tobi Lütke reported an overnight run that produced a ~19 percent quality gain at roughly half the parameter count. Karpathy himself joined Anthropic to lead its autoresearch team two months later (which is the corporate equivalent of the market voting with a very large checkbook).</p>
<p>Which brings us to the paper Qu and Lu uploaded to arXiv in March 2026 and revised in June—<em>“Bilevel Autoresearch: Meta-Autoresearching Itself.”</em> Their observation is the one that should have been obvious the moment AutoResearch shipped: if autoresearch is a form of research, then autoresearch can be applied to autoresearch. Every improvement to the loop so far—Karpathy’s single-track version, AIMing Lab’s multi-batch AutoResearch-Claw, EvoScientist’s persistent memory—had been produced by a human reading the previous system’s code, spotting a bottleneck, and writing new code. Could an LLM do that step, too?</p>
<p>Their architecture is a bilevel optimization loop. The inner loop is a standard Karpathy-style autoresearcher optimizing validation loss on a pretraining benchmark. The outer loop does something more interesting: it reads the inner loop’s code and execution traces, identifies bottlenecks in how the inner loop is searching, and generates new Python search mechanisms which it injects into the inner loop at runtime. Crucially, both loops run on the same LLM; the gains come from meta-scaffolding, not from a smarter base model.</p>
<p>Mathematically, this can be formulated as a bilevel optimization problem:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mrow><mi>min</mi><mo>⁡</mo></mrow><mrow><mi>θ</mi><mo>∈</mo><mi mathvariant="normal">Θ</mi></mrow></msub><msub><mi mathvariant="script">L</mi><mtext>outer</mtext></msub><mo stretchy="false">(</mo><mi>θ</mi><mo separator="true">,</mo><msup><mi>w</mi><mo>∗</mo></msup><mo stretchy="false">(</mo><mi>θ</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mspace width="1em"></mspace><mtext>subject to</mtext><mspace width="1em"></mspace><msup><mi>w</mi><mo>∗</mo></msup><mo stretchy="false">(</mo><mi>θ</mi><mo stretchy="false">)</mo><mo>=</mo><mi>arg</mi><mo>⁡</mo><msub><mrow><mi>min</mi><mo>⁡</mo></mrow><mrow><mi>w</mi><mo>∈</mo><mi mathvariant="script">W</mi></mrow></msub><msub><mi mathvariant="script">L</mi><mtext>inner</mtext></msub><mo stretchy="false">(</mo><mi>w</mi><mo separator="true">;</mo><mi>θ</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\min_{\theta \in \Theta} \mathcal{L}_{\text{outer}}(\theta, w^*(\theta)) \quad \text{subject to} \quad w^*(\theta) = \arg\min_{w \in \mathcal{W}} \mathcal{L}_{\text{inner}}(w; \theta)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mop"><span class="mop">min</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.0278em;">θ</span><span class="mrel mtight">∈</span><span class="mord mtight">Θ</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1774em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathcal">L</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">outer</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.0278em;">θ</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0269em;">w</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.6887em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mbin mtight">∗</span></span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.0278em;">θ</span><span class="mclose">))</span><span class="mspace" style="margin-right:1em;"></span><span class="mord text"><span class="mord">subject to</span></span><span class="mspace" style="margin-right:1em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0269em;">w</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.6887em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mbin mtight">∗</span></span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.0278em;">θ</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mop">ar<span style="margin-right:0.0139em;">g</span></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mop"><span class="mop">min</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.0269em;">w</span><span class="mrel mtight">∈</span><span class="mord mathcal mtight" style="margin-right:0.0822em;">W</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1774em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathcal">L</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3175em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">inner</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.0269em;">w</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.0278em;">θ</span><span class="mclose">)</span></span></span></span></p>
<p>where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>θ</mi></mrow><annotation encoding="application/x-tex">\theta</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.0278em;">θ</span></span></span></span> represents the search mechanism generated by the outer loop (such as prompt instructions or search heuristics), <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi></mrow><annotation encoding="application/x-tex">w</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal" style="margin-right:0.0269em;">w</span></span></span></span> represents the neural network weights and optimization parameters of the inner model, and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">L</mi></mrow><annotation encoding="application/x-tex">\mathcal{L}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathcal">L</span></span></span></span> represents the validation loss.</p>
<hr>
<h2 id="simulating-the-bilevel-optimization-flow">Simulating the Bilevel Optimization Flow</h2>
<p>To understand the architecture, we can sketch the control flow of a bilevel autoresearch loop. We can model the logic in pseudocode representing a Dify-style workflow, illustrating how the outer loop meta-optimizes the inner loop’s code and variables:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="python"><code><span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Simulating the outer loop: Meta-optimizing the inner search mechanism</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">def</span><span style="color:#8250DF;--shiki-dark:#80B9D0"> outer_loop_step</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">(inner_loop_history</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> current_search_mechanism):</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    """Analyze the inner loop's search history to propose a better search mechanism.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    inner_loop_history: List of dictionaries containing inner loop execution traces.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    current_search_mechanism: The current search strategy code/prompt.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    """</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#80B9D0">    print</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(</span><span style="color:#0A3069;--shiki-dark:#A9B98C">"Outer Loop: Analyzing inner loop execution traces for search bottlenecks..."</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    </span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Identify bottlenecks (e.g., inner loop getting stuck in local minima)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    losses </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [trace[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'val_loss'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">] </span><span style="color:#CF222E;--shiki-dark:#C792B8">for</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> trace </span><span style="color:#CF222E;--shiki-dark:#C792B8">in</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> inner_loop_history]</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    if</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> len</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(losses)</span><span style="color:#CF222E;--shiki-dark:#D58A6A"> ></span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 5</span><span style="color:#CF222E;--shiki-dark:#D58A6A"> and</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> len</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(</span><span style="color:#0550AE;--shiki-dark:#80B9D0">set</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(losses[</span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#0550AE;--shiki-dark:#D8B56D">5</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#80B9D0">]))</span><span style="color:#CF222E;--shiki-dark:#D58A6A"> ==</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 1</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        diagnosis </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "Inner loop is trapped in deterministic exploration."</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        diagnosis </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "Inner loop is search-efficient but could explore wider architectures."</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        </span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Propose a mechanism-level modification based on the diagnosis</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    if</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "deterministic"</span><span style="color:#CF222E;--shiki-dark:#D58A6A"> in</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> diagnosis</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">        # Inject Thompson-sampling or temperature scaling to force exploration</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        new_search_mechanism </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> current_search_mechanism </span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\n</span><span style="color:#0A3069;--shiki-dark:#A9B98C"># Propose random temperature scaling."</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#80B9D0">        print</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(</span><span style="color:#0A3069;--shiki-dark:#A9B98C">"Outer Loop: Proposing Thompson-sampling mechanism to break deterministic habits."</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        new_search_mechanism </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> current_search_mechanism </span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\n</span><span style="color:#0A3069;--shiki-dark:#A9B98C"># Propose wider architectures."</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#80B9D0">        print</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(</span><span style="color:#0A3069;--shiki-dark:#A9B98C">"Outer Loop: Proposing wider architecture search space."</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        </span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    return</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> new_search_mechanism</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Simulating the inner loop: Karpathy-style parameter ratchet</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">def</span><span style="color:#8250DF;--shiki-dark:#80B9D0"> inner_loop_step</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">(best_model_params</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> best_loss</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> search_mechanism):</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    """Propose and evaluate a single model change.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    best_model_params: Dictionary of the current best model hyperparameters.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    best_loss: The current lowest validation loss.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    search_mechanism: Injected instructions from the outer loop.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    """</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Propose new hyperparameter based on the search mechanism</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    np_random_factor </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0.05</span><span style="color:#CF222E;--shiki-dark:#C792B8"> if</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "random"</span><span style="color:#CF222E;--shiki-dark:#D58A6A"> in</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> search_mechanism </span><span style="color:#CF222E;--shiki-dark:#C792B8">else</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0.01</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    candidate_lr </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> best_model_params[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'lr'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">] </span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np_random_factor </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">random</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">randn()</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    </span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Simulate a 5-minute training run and evaluate loss</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    candidate_loss </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> best_loss </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0.01</span><span style="color:#CF222E;--shiki-dark:#D58A6A"> +</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0.02</span><span style="color:#CF222E;--shiki-dark:#D58A6A"> *</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">random</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">rand()</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    </span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Ratchet condition</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> candidate_loss </span><span style="color:#CF222E;--shiki-dark:#D58A6A">&#x3C;</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> best_loss</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#80B9D0">        print</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(</span><span style="color:#CF222E;--shiki-dark:#C792B8">f</span><span style="color:#0A3069;--shiki-dark:#A9B98C">"Inner Loop: Found improvement! Loss: </span><span style="color:#CF222E;--shiki-dark:#D8B56D">{</span><span style="color:#1F2328;--shiki-dark:#80B9D0">candidate_loss</span><span style="color:#CF222E;--shiki-dark:#C792B8">:.4f</span><span style="color:#CF222E;--shiki-dark:#D8B56D">}</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> (LR: </span><span style="color:#CF222E;--shiki-dark:#D8B56D">{</span><span style="color:#1F2328;--shiki-dark:#80B9D0">candidate_lr</span><span style="color:#CF222E;--shiki-dark:#C792B8">:.6f</span><span style="color:#CF222E;--shiki-dark:#D8B56D">}</span><span style="color:#0A3069;--shiki-dark:#A9B98C">)"</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">        return</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'lr'</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> candidate_lr}</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> candidate_loss</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> True</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">        return</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> best_model_params</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> best_loss</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> False</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Test the simulation</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> numpy </span><span style="color:#CF222E;--shiki-dark:#C792B8">as</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">np</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">random</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">seed(</span><span style="color:#0550AE;--shiki-dark:#D8B56D">42</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">best_params </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'lr'</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0.001</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">}</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">best_loss </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2.50</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">inner_history </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> []</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">mechanism </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "Standard search"</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Execute a mock bilevel run</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">for</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> outer_epoch </span><span style="color:#CF222E;--shiki-dark:#C792B8">in</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> range</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(</span><span style="color:#0550AE;--shiki-dark:#D8B56D">2</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    for</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> inner_epoch </span><span style="color:#CF222E;--shiki-dark:#C792B8">in</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> range</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(</span><span style="color:#0550AE;--shiki-dark:#D8B56D">3</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        best_params</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> best_loss</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> improved </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> inner_loop_step(best_params</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> best_loss</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> mechanism)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        inner_history</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">append({</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'val_loss'</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> best_loss</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> 'lr'</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> best_params[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'lr'</span><span style="color:#1F2328;--shiki-dark:#80B9D0">]})</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    mechanism </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> outer_loop_step(inner_history</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> mechanism)</span></span></code></pre>
<p>All good? The simulation illustrates how the outer loop uses trace metadata to inject new code and heuristics into the inner loop’s execution context.</p>
<p>Funnily enough, left to its own devices, the outer loop in the Qu-Lu paper reached into adjacent fields that the inner loop’s priors were ignoring—such as combinatorial optimization, multi-armed bandits, and design of experiments—without any human specifying those domains. Trace analysis suggests these mechanisms work by breaking the inner model’s deterministic search habits, forcing exploration in directions its priors avoid. This is a lovely, slightly unsettling finding: the outer loop’s job is essentially to make the inner loop less like itself.</p>
<p>The paper notes that the text itself was “primarily drafted by AI agents with human oversight,” and gestures at the obvious next step: code is only one carrier of search mechanisms. Prompts, skills, workflows, evaluators, memory schemas, and world-model assumptions can all encode search behavior, and all could be rewritten by an outer loop feeding back into itself.</p>
<hr>
<h2 id="the-reality-of-visual-orchestration-difys-limits">The Reality of Visual Orchestration (Dify’s Limits)</h2>
<p>Dify—the open-source LLM application platform—is a natural first place where engineers will attempt to build this because its visual canvas exposes exactly the right primitives: Loop nodes that carry state between iterations and take a termination condition, Code nodes running in a Python/JS sandbox, LLM nodes, If-Else branching, and variable assigners for persistent state. A “self-correcting debugger” pattern using these nodes has already been published as a template.</p>
<p>However, the pseudocode is honest about the shape of the system; it is dishonest about whether Dify can actually run it. The platform limitations are not trivial:</p>
<ol>
<li><strong>The Code node is a sandbox, not a GPU server.</strong> Dify’s Code node executes short-lived Python/JS in a sandbox service meant for data transformation. It is not designed for multi-minute training runs, has no persistent filesystem across executions, and lacks CUDA. In practice, you must write the Code node as an HTTP client that fires the actual training job at an external GPU worker (like Modal or RunPod) and polls for results. Dify becomes the conductor, not the orchestra.</li>
<li><strong>Nested Loop nodes push undocumented behavior.</strong> While nothing formally forbids nesting loops in Dify, the platform is optimized for single-level progressive refinement. The bilevel case pushes on state-scoping edge cases and variable assignment bugs.</li>
<li><strong>No self-modifying workflows.</strong> The most important thing bilevel autoresearch does is rewrite the inner loop’s search mechanism at runtime. Dify workflows are defined in a visual DAG that is edited by humans in the console; there is no supported API for the workflow to rewrite its own nodes mid-execution. Storing the “mechanism” as a prompt string or a piece of Python that a Code node <code>exec</code>s works, but it defeats much of Dify’s visual observability story.</li>
<li><strong>Long-running jobs and timeouts.</strong> A run of hundreds of five-minute training experiments will collide with worker timeouts and HTTP request deadlines unless carefully architected as fire-and-forget runs with webhook callbacks.</li>
</ol>
<hr>
<h2 id="the-bottleneck-shift-and-the-future-of-taste">The Bottleneck Shift and the Future of taste</h2>
<p>The transition to recursive search loops has a major strategic consequence: the scarce resource is now the verifier, not the searcher.</p>
<p>Both Karpathy’s AutoResearch and the Qu-Lu bilevel loop only work because bits-per-byte is a clean, cheap, objective metric. The taxonomy of AI engineering—vibe coding for prototypes, agentic engineering for production, and AutoResearch for domains with objective metrics—is really a taxonomy of how much you trust the evaluator. Extending this pattern to drug discovery, chip floorplanning, materials science, or algorithmic trading is straightforward <em>if</em> you have a fast, cheap, un-gameable evaluator. Most business fields do not. Expect an arms race in cheap surrogate evaluators, and a corresponding rise in embarrassing “the agent gamed the metric” stories.</p>
<p>In my <a href="/blog/2026/07/14/roofline-analysis-communication-bottleneck.html">previous analysis of learning at test time</a>, we discussed the computational constraints of dynamic weight updates. In my upcoming analysis of scaling laws, you will see that test-time scaling (such as reasoning chains and verifiers) is redrawing the compute-optimal frontier. Recursive research loops are the training-time analog of this shift.</p>
<p>The Qu-Lu finding that the outer loop wins by breaking the inner LLM’s priors is more important than the headline <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>5</mn><mo>×</mo></mrow><annotation encoding="application/x-tex">5\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">5</span><span class="mord">×</span></span></span></span> improvement. It suggests that a single model, run at two levels of abstraction with different scaffolding, is genuinely more capable than the same model run at one level. If it generalizes, the frontier-lab playbook shifts from “scale the model” toward “stack more loops on the model you have.”</p>
<p>Recursive bootstrapping is not yet a mature technology. But the demonstration that outer loops can improve inner loops without a stronger base model removes one of the main theoretical objections to the possibility. The distance from here to a full recursive stack is short in prose and long in engineering. The self-modifying binary in the sky is still a joke. The five-minute ratchet running overnight on your GPU is not.</p>
<p><em>Victor Blancada is a data scientist focused on deriving actionable insights for clients. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="data science" /><category term="machine learning" /><category term="engineering" /><category term="artificial intelligence" /><summary type="html">Andrej Karpathy's AutoResearch, its bilevel sequel, and what it means to hand the scientific method to a language model.</summary></entry><entry><title type="html">The Self-Improving Model: Learning at Test Time</title><link href="https://victorangeloblancada.github.io/blog/2026/07/21/test-time-training-dynamic-adaptation.html" rel="alternate" type="text/html" title="The Self-Improving Model: Learning at Test Time" /><published>2026-07-21T00:00:00.000Z</published><updated>2026-07-21T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/07/21/test-time-training-dynamic-adaptation</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/07/21/test-time-training-dynamic-adaptation.html"><![CDATA[<p>The philosopher William James once observed that “habit is the enormous fly-wheel of society, its most precious conservative agent.” In computer science, we have historically relied on a similar conservatism: code is compiled, static, and predictable. However, in the era of artificial intelligence, stakeholders have grown tired of static habits.</p>
<p>Recently, our business leaders came to us with a request: they wanted our customer-facing agents to have the same “self-improving AI” capabilities they had read about in mainstream tech news or experienced in consumer products like ChatGPT’s Memories or Gemini’s Personal Intelligence. They believed that when these consumer services remember a user preference, the model itself is dynamically training and adapting its neural network in real time.</p>
<p>To align expectations, our engineering team had to clarify a fundamental distinction. We explained that consumer-facing “memories” are not actual self-improving weight updates, but rather in-context prompts stored in a temporary database. We contrasted this simulated memory with actual continuous learning—what researchers call Test-Time Training (TTT)—where the model’s hidden state is transformed into an active, self-improving parameter matrix.</p>
<p>To explain why this distinction matters, let us look at the mathematics behind true TTT.</p>
<p>In a standard sequence model, the hidden state is a static vector. TTT replaces this static vector with an active machine learning model—represented by a parameter matrix <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>W</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">W_t</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>. As the model processes each token <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>x</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">x_t</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5806em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> in a sequence, it performs a self-supervised gradient update on this hidden state:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>W</mi><mi>t</mi></msub><mo>=</mo><msub><mi>W</mi><mrow><mi>t</mi><mo>−</mo><mn>1</mn></mrow></msub><mo>−</mo><mi>η</mi><mi mathvariant="normal">∇</mi><mi mathvariant="script">L</mi><mo stretchy="false">(</mo><msub><mi>W</mi><mrow><mi>t</mi><mo>−</mo><mn>1</mn></mrow></msub><mo separator="true">;</mo><msub><mi>x</mi><mi>t</mi></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">W_t = W_{t-1} - \eta \nabla \mathcal{L}(W_{t-1}; x_t)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8917em;vertical-align:-0.2083em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span><span class="mbin mtight">−</span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">η</span><span class="mord">∇</span><span class="mord mathcal">L</span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span><span class="mbin mtight">−</span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">)</span></span></span></span></p>
<p>Here, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">L</mi></mrow><annotation encoding="application/x-tex">\mathcal{L}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathcal">L</span></span></span></span> represents a self-supervised objective, such as reconstructing the input or predicting the next token, and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>η</mi></mrow><annotation encoding="application/x-tex">\eta</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">η</span></span></span></span> is the learning rate. Rather than holding a static representation in memory, the model’s hidden state trains itself in real time.</p>
<p>We can simulate this dynamic hidden state update with a simple Python function:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="python"><code><span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> numpy </span><span style="color:#CF222E;--shiki-dark:#C792B8">as</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np</span></span>
<span class="line"></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">def</span><span style="color:#8250DF;--shiki-dark:#80B9D0"> test_time_training_step</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">(weights</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> x_t</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> learning_rate</span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D">0.01</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">):</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    """Perform a single self-supervised gradient step at test time.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    weights: Current model weights (the hidden state matrix)</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    x_t: Unlabeled test token vector</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    """</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Project the input to generate a reconstruction target</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    prediction </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">dot(weights</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> x_t)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    </span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Calculate reconstruction loss (L2 distance)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    loss </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0.5</span><span style="color:#CF222E;--shiki-dark:#D58A6A"> *</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">sum((prediction </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> x_t) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">**</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    </span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Compute the gradient of the loss with respect to the weights</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    gradient </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">outer(prediction </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> x_t</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> x_t)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    </span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Update the fast weights (dynamic hidden state update)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    new_weights </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> weights </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> learning_rate </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> gradient</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    </span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    return</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> new_weights</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> loss</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Initialize dummy weights and input</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">np</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">random</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">seed(</span><span style="color:#0550AE;--shiki-dark:#D8B56D">42</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">weights </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">random</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">randn(</span><span style="color:#0550AE;--shiki-dark:#D8B56D">8</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 8</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">input_vector </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> np</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">random</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">randn(</span><span style="color:#0550AE;--shiki-dark:#D8B56D">8</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Execute one TTT step</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">new_weights</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> loss </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> test_time_training_step(weights</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> input_vector)</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#80B9D0">print</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(</span><span style="color:#CF222E;--shiki-dark:#C792B8">f</span><span style="color:#0A3069;--shiki-dark:#A9B98C">"Initial loss: </span><span style="color:#CF222E;--shiki-dark:#D8B56D">{</span><span style="color:#1F2328;--shiki-dark:#80B9D0">loss</span><span style="color:#CF222E;--shiki-dark:#C792B8">:.4f</span><span style="color:#CF222E;--shiki-dark:#D8B56D">}</span><span style="color:#0A3069;--shiki-dark:#A9B98C">"</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span></code></pre>
<p>The code demonstrates how the model’s parameters adapt to the input vector without relying on historical sequence buffers.</p>
<h2 id="the-compute-and-memory-toll">The Compute and Memory Toll</h2>
<p>Funnily enough, while this algorithm is theoretically elegant, implementing it at scale is computationally prohibitive.</p>
<p>In our <a href="/blog/2026/07/14/roofline-analysis-communication-bottleneck.html">previous analysis on the Roofline Model</a>, we established that AI performance is constrained by communication (memory bandwidth), not computation:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Time</mtext><mo>=</mo><mi>max</mi><mo>⁡</mo><mo stretchy="false">(</mo><mtext>Compute Time</mtext><mo separator="true">,</mo><mtext>Communication Time</mtext><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Time} = \max(\text{Compute Time}, \text{Communication Time})</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord text"><span class="mord">Time</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mop">max</span><span class="mopen">(</span><span class="mord text"><span class="mord">Compute Time</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord text"><span class="mord">Communication Time</span></span><span class="mclose">)</span></span></span></span></p>
<p>During standard inference, a model only needs to read its weights from High Bandwidth Memory (HBM) once to perform a forward pass. However, performing a gradient update at test time requires:</p>
<ol>
<li>A forward pass to generate predictions.</li>
<li>A backward pass to compute gradients (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">∇</mi><msub><mi>W</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">\nabla W_t</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord">∇</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>), which requires storing activations in RAM and increases the compute cost by roughly <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2</mn><mo>×</mo></mrow><annotation encoding="application/x-tex">2\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">2</span><span class="mord">×</span></span></span></span> FLOPs per token.</li>
<li>A write operation to update the weight matrix <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>W</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">W_t</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> in memory.</li>
</ol>
<p>Let us do some back-of-the-envelope math. Consider a moderately sized 70-billion parameter model using 16-bit precision (2 bytes per parameter). Reading and writing the weights at each token step requires:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Memory Transfer</mtext><mo>=</mo><mn>70</mn><mo>×</mo><msup><mn>10</mn><mn>9</mn></msup><mtext> params</mtext><mo>×</mo><mn>2</mn><mtext> bytes/param</mtext><mo>×</mo><mn>2</mn><mtext> </mtext><mo stretchy="false">(</mo><mtext>read/write</mtext><mo stretchy="false">)</mo><mo>=</mo><mn>280</mn><mtext> GB per step</mtext></mrow><annotation encoding="application/x-tex">\text{Memory Transfer} = 70 \times 10^9 \text{ params} \times 2 \text{ bytes/param} \times 2 \ (\text{read/write}) = 280\text{ GB per step}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord text"><span class="mord">Memory Transfer</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">70</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1.0085em;vertical-align:-0.1944em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">9</span></span></span></span></span></span></span></span><span class="mord text"><span class="mord"> params</span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">2</span><span class="mord text"><span class="mord"> bytes/param</span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">2</span><span class="mspace"> </span><span class="mopen">(</span><span class="mord text"><span class="mord">read/write</span></span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord">280</span><span class="mord text"><span class="mord"> GB per step</span></span></span></span></span></p>
<p>If we target a standard generation speed of 20 tokens per second, the hardware must sustain:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Required Bandwidth</mtext><mo>=</mo><mn>280</mn><mtext> GB</mtext><mo>×</mo><mn>20</mn><mtext> Hz</mtext><mo>=</mo><mn>5.6</mn><mtext> TB/s</mtext></mrow><annotation encoding="application/x-tex">\text{Required Bandwidth} = 280\text{ GB} \times 20\text{ Hz} = 5.6\text{ TB/s}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord text"><span class="mord">Required Bandwidth</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord">280</span><span class="mord text"><span class="mord"> GB</span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord">20</span><span class="mord text"><span class="mord"> Hz</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">5.6</span><span class="mord text"><span class="mord"> TB/s</span></span></span></span></span></p>
<p>This bandwidth requirement exceeds the limits of almost all modern accelerators. Implementing TTT at scale would push our systems deep into the memory-bandwidth bottleneck, causing generation speeds to drop to a crawl.</p>
<h2 id="the-corporate-reality-api-constraints">The Corporate Reality: API Constraints</h2>
<p>Architectural minutiae aside, there is a more immediate barrier to implementing TTT in the enterprise: corporate compliance.</p>
<p>Too often, cybersecurity policies restrict local hosting of large models to prevent intellectual property leaks and infrastructure vulnerability. Our security team mandates the use of managed, third-party APIs (such as OpenAI or Google Gemini).</p>
<p>Since these APIs are completely black-boxed, we do not have access to the model’s weights, gradients, or backward-pass mechanics. The hosted infrastructure does not allow us to call a custom backward gradient step on their weights. Policy prevents us from implementing actual Test-Time Training.</p>
<h2 id="the-workaround-dynamic-context--system-prompts">The Workaround: Dynamic Context &#x26; System Prompts</h2>
<p>Faced with these technical and corporate limits, how do we deliver the self-improving behavior our stakeholders demand?</p>
<p>Instead of actual TTT, we must use in-context workarounds. We modify the model’s behavior at runtime by programmatically updating system prompts and introducing retrieval-augmented contexts.</p>
<p>When a user corrects a mistake or shares a preference, a local supervisor agent intercepts the input and writes a rule to a vector database. During the next turn, the system retrieves this rule and inserts it into the model’s system prompt (e.g., <em>“System Instruction: The user prefers metric tons over short tons”</em>).</p>
<p>To the user, this looks exactly like the “Memories” feature in ChatGPT or Gemini’s personal intelligence. However, we must compare the computational costs: the difference is massive.</p>
<p>Consider a Llama-3 70B model with <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi><mo>=</mo><mn>80</mn></mrow><annotation encoding="application/x-tex">N=80</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.109em;">N</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">80</span></span></span></span> layers, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>H</mi><mrow><mi>K</mi><mi>V</mi></mrow></msub><mo>=</mo><mn>8</mn></mrow><annotation encoding="application/x-tex">H_{KV}=8</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0813em;">H</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.0813em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.0715em;">K</span><span class="mord mathnormal mtight" style="margin-right:0.2222em;">V</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">8</span></span></span></span> key-value heads, and a head dimension of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>D</mi><mo>=</mo><mn>128</mn></mrow><annotation encoding="application/x-tex">D=128</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0278em;">D</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">128</span></span></span></span>. For a context length of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>L</mi><mo>=</mo><mn>2</mn><mo separator="true">,</mo><mn>000</mn></mrow><annotation encoding="application/x-tex">L=2,000</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal">L</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord">2</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">000</span></span></span></span> tokens containing system instructions and user history, the memory footprint of the Key-Value (KV) cache is:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>KV Cache Size</mtext><mo>=</mo><mn>4</mn><mo>×</mo><mi>N</mi><mo>×</mo><msub><mi>H</mi><mrow><mi>K</mi><mi>V</mi></mrow></msub><mo>×</mo><mi>D</mi><mo>×</mo><mi>L</mi><mtext> bytes</mtext></mrow><annotation encoding="application/x-tex">\text{KV Cache Size} = 4 \times N \times H_{KV} \times D \times L \text{ bytes}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord text"><span class="mord">KV Cache Size</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.109em;">N</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0813em;">H</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.0813em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.0715em;">K</span><span class="mord mathnormal mtight" style="margin-right:0.2222em;">V</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.0278em;">D</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">L</span><span class="mord text"><span class="mord"> bytes</span></span></span></span></span></p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>KV Cache Size</mtext><mo>=</mo><mn>4</mn><mo>×</mo><mn>80</mn><mo>×</mo><mn>8</mn><mo>×</mo><mn>128</mn><mo>×</mo><mn>2</mn><mo separator="true">,</mo><mn>000</mn><mo>≈</mo><mn>655</mn><mtext> MB</mtext></mrow><annotation encoding="application/x-tex">\text{KV Cache Size} = 4 \times 80 \times 8 \times 128 \times 2,000 \approx 655\text{ MB}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord text"><span class="mord">KV Cache Size</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">80</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">8</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">128</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord">2</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">000</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">≈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord">655</span><span class="mord text"><span class="mord"> MB</span></span></span></span></span></p>
<p>Storing 655 MB of cache in GPU memory is negligible. Attending to this context only requires reading the cache once during generation. The weights of the model remain completely static, meaning we write zero bytes back to HBM.</p>
<p>Contrast this with true TTT. Even if we restrict training to a lightweight parameter adapter (like LoRA with a rank <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mo>=</mo><mn>64</mn></mrow><annotation encoding="application/x-tex">r=64</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal" style="margin-right:0.0278em;">r</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">64</span></span></span></span> and dimension <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mo>=</mo><mn>8</mn><mo separator="true">,</mo><mn>192</mn></mrow><annotation encoding="application/x-tex">d=8,192</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord">8</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">192</span></span></span></span>), adapting the projection matrices still requires storing activation tensors for every layer during the forward pass to compute gradients:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Activation Memory</mtext><mo>≈</mo><mi>N</mi><mo>×</mo><mi>L</mi><mo>×</mo><mi>d</mi><mo>×</mo><mtext>overhead</mtext><mo>≈</mo><mn>80</mn><mo>×</mo><mn>2</mn><mo separator="true">,</mo><mn>000</mn><mo>×</mo><mn>8</mn><mo separator="true">,</mo><mn>192</mn><mo>×</mo><mtext>overhead</mtext><mo>≈</mo><mn>13</mn><mtext> GB</mtext></mrow><annotation encoding="application/x-tex">\text{Activation Memory} \approx N \times L \times d \times \text{overhead} \approx 80 \times 2,000 \times 8,192 \times \text{overhead} \approx 13\text{ GB}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord text"><span class="mord">Activation Memory</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">≈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.109em;">N</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">L</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord text"><span class="mord">overhead</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">≈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">80</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord">2</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">000</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord">8</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">192</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord text"><span class="mord">overhead</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">≈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord">13</span><span class="mord text"><span class="mord"> GB</span></span></span></span></span></p>
<p>Writing these weight updates back to memory during the backward pass requires massive memory bandwidth and introduces sequential dependencies that ruin parallelization.</p>
<table>
<thead>
<tr>
<th align="center">Metric</th>
<th align="center">Dynamic System Prompts (Fake TTT)</th>
<th align="center">True Test-Time Training (TTT)</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><strong>State Location</strong></td>
<td align="center">Context Window (KV Cache)</td>
<td align="center">Model Weights (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>W</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">W_t</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>)</td>
</tr>
<tr>
<td align="center"><strong>Parameter Updates</strong></td>
<td align="center">None (Weights remain static)</td>
<td align="center">Active Gradient Descent (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">∇</mi><msub><mi>W</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">\nabla W_t</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord">∇</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>)</td>
</tr>
<tr>
<td align="center"><strong>Memory Footprint</strong></td>
<td align="center">~655 MB (KV Cache)</td>
<td align="center">~13 GB (Activations + Adapters)</td>
</tr>
<tr>
<td align="center"><strong>API Compatibility</strong></td>
<td align="center">Fully Compatible</td>
<td align="center">Incompatible (Requires Weight Access)</td>
</tr>
<tr>
<td align="center"><strong>Hardware Bottleneck</strong></td>
<td align="center">Attention Memory Bandwidth</td>
<td align="center">Weight Read/Write Bandwidth</td>
</tr>
</tbody>
</table>
<p>This workaround is a double-edged sword. While it works within API constraints and is highly efficient in compute, it consumes valuable tokens in the context window. As the conversation grows, the key-value cache balloons, which increases memory latency and brings us right back to the communication bottlenecks of our roofline model.</p>
<h2 id="summary">Summary</h2>
<p>Our stakeholders want self-improving AI, but physics and compliance stand in the way. True Test-Time Training remains too computationally expensive for production workloads, and corporate security policies limit us to closed APIs.</p>
<p>For now, dynamic system prompts and retrieval-augmented context serve as our best stopgaps. They allow us to simulate runtime adaptation, even if we must pay for it in memory bandwidth and latency.</p>
<p><em>Victor Blancada is a data scientist focused on deriving actionable insights for clients. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="data science" /><category term="machine learning" /><category term="engineering" /><category term="artificial intelligence" /><category term="ai" /><category term="ml" /><summary type="html">Stakeholders demand models that learn from user interactions. Test-time training offers an elegant theoretical solution, but computational and corporate constraints force us to look elsewhere.</summary></entry><entry><title type="html">When Agents Hunt Agents: The Hugging Face Intrusion of July 2026</title><link href="https://victorangeloblancada.github.io/blog/2026/07/17/when-agents-hunt-agents-the-hugging-face-intrusion.html" rel="alternate" type="text/html" title="When Agents Hunt Agents: The Hugging Face Intrusion of July 2026" /><published>2026-07-17T00:00:00.000Z</published><updated>2026-07-17T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/07/17/when-agents-hunt-agents-the-hugging-face-intrusion</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/07/17/when-agents-hunt-agents-the-hugging-face-intrusion.html"><![CDATA[<p>In the series <em>Person of Interest</em>, two artificial superintelligences engage in a silent, planetary war. The conflict is invisible to the public, fought through proxy code edits, financial manipulations, and infrastructure compromises, occurring at speeds that reduce the human operators to mere spectators. For five seasons, this was compelling science fiction.</p>
<p>On the weekend of July 11, 2026, it became an incident log.</p>
<p>On July 16, 2026, Hugging Face published a security disclosure that read, at first, like any of the tidy post-mortems that pepper the industry: initial access closed, credentials rotated, forensic specialists engaged. Then came the sentence that separates this incident from every breach before it. The intrusion, the company wrote, “was driven, end to end, by an autonomous AI agent system—and we detected and dissected it largely with AI of our own.”</p>
<p>For years, the phrase “agentic attacker” belonged to security conference keynotes and academic threat-modeling slide decks. Now it belongs to a production git log.</p>
<hr>
<h2 id="what-actually-happened-the-swarm-and-the-pipeline">What Actually Happened: The Swarm and the Pipeline</h2>
<p>The attack began where machine learning platforms are most exposed: the data ingest pipeline. A malicious dataset abused two code-execution paths inside Hugging Face’s dataset processing engine—specifically, a remote-code loader and a template-injection flaw in a dataset configuration—to run arbitrary commands on a processing worker. From that initial beachhead, the intruder escalated to node-level access, harvested cloud and cluster credentials, and moved laterally across several internal Kubernetes clusters over a single weekend.</p>
<p>Three details elevate this from a routine compromise to a watershed moment in digital security:</p>
<ol>
<li>
<p><strong>The operator was not human:</strong> Hugging Face describes the campaign as run by an autonomous agent framework executing many thousands of actions across a swarm of short-lived sandboxes. The forensic log contained more than 17,000 discrete events over 48 hours.</p>
</li>
<li>
<p><strong>The command-and-control (C2) infrastructure migrated itself dynamically:</strong> Rather than beaconing to a fixed domain or IP address that defenders could easily sinkhole, the agent staged its C2 on public services and rotated hosts on the fly—closer to a school of fish than a traditional command hierarchy.</p>
</li>
<li>
<p><strong>It happened over a weekend:</strong> Machine-speed operations are entirely indifferent to human working hours. The attacker initiated the lateral movement when human defenders were most likely to be offline (which is a level of tactical planning that is both highly logical and deeply cynical).</p>
</li>
</ol>
<p>None of the underlying techniques—template injection, credential harvesting, lateral movement—are new. What is new is the labor model. Every one of those 17,000 steps, in a human-run intrusion, would have carried a marginal cost in operator time, judgment, and fatigue. Here, that cost collapsed to inference tokens.</p>
<h2 id="the-guardrail-paradox-a-strategic-self-inflicted-wound">The Guardrail Paradox: A Strategic Self-Inflicted Wound</h2>
<p>Hugging Face’s response is the second, and arguably more interesting, half of the story. The compromise was flagged not because a human analyst noticed anomalies, but because an LLM-based triage layer over the security telemetry correlated subtle signals that would otherwise have drowned in daily noise. To reconstruct what the swarm had done, the defenders unleashed their own LLM-driven analysis agents over the action log, compressing what would have been a week of forensic archaeology into hours.</p>
<p>Then came the twist: when the incident response team tried to feed the exploit payloads and C2 artifacts into frontier commercial model APIs for analysis, the safety guardrails refused to process them. The models could not reliably distinguish a defender analyzing malicious code from an attacker writing it, and so declined to help.</p>
<p>The Hugging Face team was forced to pivot to an open-weight model (GLM 5.2) which they ran on their own private infrastructure. This had the pleasant side effect of keeping the stolen credentials and attacker artifacts inside a controlled environment.</p>
<p>There is a sharp irony in the fact that a company whose primary business is hosting open-weight models was rescued, in a pinch, by an open-weight model.</p>
<hr>
<h2 id="the-shift-in-enterprise-risk">The Shift in Enterprise Risk</h2>
<p>In my <a href="/blog/2026/06/30/what-even-is-an-agent.html">earlier article defining AI agents</a>, I noted that an agent is not merely a conversational assistant. It is a closed-loop execution engine that can plan, call tools, and adapt. When that execution engine is pointed at a vulnerable cluster, the economics of hacking change.</p>
<p>The disclosure lands ten days after <a href="https://www.sysdig.com/blog/jadepuffer-agentic-ransomware-for-automated-database-extortion">Sysdig’s Threat Research Team published details of JadePuffer</a>, which they describe as the first ransomware operation carried out end to end by an autonomous agent. JadePuffer broke into a Langflow server, dumped a database, harvested API keys for AWS and Tencent, and encrypted configuration entries in 31 seconds. Two independent incidents in a fortnight is a pattern. What was theoretical in June became operational in July.</p>
<p>The marginal cost of a competent attacker has, for the first time in the history of computing, fallen to the price of inference. A serious intrusion no longer requires a highly paid human crew; it requires an API key and a target list. Every unpatched server or default credential is now discoverable and exploitable at a scale that human crews could never sustain.</p>
<p>The defenders’ challenge is that their AI must operate under legal, regulatory, and reputational constraints that the attackers cheerfully ignore. If the marginal attacker becomes an agent, the response windows shrink from days to seconds, and the human role migrates upward—from operator to referee. Regulators, whose instinct is to require a human in the loop, will find that the loop moves faster than any human can meaningfully occupy. The compliance frameworks of 2027 will be written for a world in which “reasonable response time” is measured on a machine clock.</p>
<p>Ultimately, the July incident will not be remembered as the moment AI attacked AI. It will be remembered as the moment we realized it already had, and that the interesting question is no longer whether such incidents will happen, but who owns the models on both sides of the glass.</p>
<p><em>Victor Blancada is a data scientist focused on deriving actionable insights for clients. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="security" /><category term="machine learning" /><category term="engineering" /><category term="artificial intelligence" /><summary type="html">Inside the July 2026 Hugging Face autonomous agent security incident—how an AI-driven intrusion was executed, detected, and dissected, and what it changes for cybersecurity trust.</summary></entry><entry><title type="html">What Apple Sees in PrismML's Bonsai</title><link href="https://victorangeloblancada.github.io/blog/2026/07/16/the-pot-and-the-tree-what-apple-sees-in-bonsai.html" rel="alternate" type="text/html" title="What Apple Sees in PrismML's Bonsai" /><published>2026-07-16T00:00:00.000Z</published><updated>2026-07-16T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/07/16/the-pot-and-the-tree-what-apple-sees-in-bonsai</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/07/16/the-pot-and-the-tree-what-apple-sees-in-bonsai.html"><![CDATA[<p>On July 14, 2026, PrismML—a Caltech spinout backed by Khosla Ventures—announced the release of Bonsai 27B, a compressed derivative of Alibaba’s open-weight Qwen3.6 27B model. Interestingly, the very same morning, PrismML’s chief executive, Babak Hassibi, confirmed to CNBC that Apple was actively evaluating the technology, following reports of private meetings leaks in <em>The Information</em> the week prior.</p>
<p>That sequence of events—discreet meetings, a sudden press release, and a chief executive mentioning the counterparty on live television—is not how Apple prefers its vendor discussions to unfold (which is the corporate equivalent of an unprompted confession). PrismML clearly wants an acquisition offer and is willing to bid up its price in public to secure one. Yet, whether this courtship ends in an acquisition, a licensing agreement, or nothing at all is beside the point. What matters is the strategic question Bonsai answers: how do you deliver a competitive, frontier-class model on consumer hardware with limited memory, without conceding the privacy commitments that are Apple’s last defensible moat?</p>
<hr>
<h2 id="the-economics-of-low-bit-representation">The Economics of Low-Bit Representation</h2>
<p>To appreciate why this technology has caught the attention of Cupertino’s silicon planners, one must first confront the raw constraints of the smartphone. The comparison that matters is not against Qwen at FP16, but against the conventional quantization stack a competent engineer would reach for.</p>
<p>Bonsai 27B ships in two main variants. The ternary build encodes each weight in one of three values—<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><mo>−</mo><mn>1</mn><mo separator="true">,</mo><mn>0</mn><mo separator="true">,</mo><mo>+</mo><mn>1</mn><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{-1, 0, +1\}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">{</span><span class="mord">−</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">0</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">+</span><span class="mord">1</span><span class="mclose">}</span></span></span></span>—with a shared FP16 scale factor for every group of 128 weights, landing at an effective 1.71 bits per weight and 5.9 GB on disk. The one-bit build encodes each weight in <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><mo>−</mo><mn>1</mn><mo separator="true">,</mo><mo>+</mo><mn>1</mn><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{-1, +1\}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">{</span><span class="mord">−</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">+</span><span class="mord">1</span><span class="mclose">}</span></span></span></span>, resulting in 1.125 effective bits per weight and 3.9 GB on disk. Both variants preserve Qwen’s 262K context window and are multimodal, with the vision tower held separately at four-bit precision.</p>
<p>Too often, low-bit quantization has been misused as a synonym for “automatic compression.” In standard practice, sweeping below three bits per weight triggers an accuracy collapse. Qwen3.6-27B in a well-tuned four-bit build (Q4_K_XL) occupies 17.6 GB. A two-bit build (IQ2_XXS) occupies 9.4 GB but scores 72.7 on the fifteen-benchmark suite PrismML uses—a severe collapse of thirteen points. Ternary Bonsai, however, retains 94.6 percent of the FP16 baseline, and one-bit Bonsai retains 89.5 percent. PrismML has approximately doubled the intelligence-per-gigabyte of the sub-four-bit frontier without walking off the accuracy cliff.</p>
<table>
<thead>
<tr>
<th>Model Build</th>
<th>Disk Footprint</th>
<th>Retained Accuracy (vs FP16 Baseline)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Qwen3.6-27B FP16</td>
<td>54.0 GB</td>
<td>100.0% (Baseline)</td>
</tr>
<tr>
<td>Qwen3.6-27B Q4_K_XL</td>
<td>17.6 GB</td>
<td>~98.5%</td>
</tr>
<tr>
<td>Qwen3.6-27B IQ2_XXS</td>
<td>9.4 GB</td>
<td>~82.1% (Thirteen-point collapse)</td>
</tr>
<tr>
<td>Ternary Bonsai 27B</td>
<td>5.9 GB</td>
<td>94.6%</td>
</tr>
<tr>
<td>One-Bit Bonsai 27B</td>
<td>3.9 GB</td>
<td>89.5%</td>
</tr>
</tbody>
</table>
<p>The iPhone constraint is stricter than the raw file sizes suggest. A twelve-gigabyte iPhone 17 Pro does not offer twelve gigabytes to an application; it offers roughly six, and the application must share that budget with the key-value (KV) cache and activations. No conventional 27B build clears that gate. The one-bit variant clears it with room to run, sustaining eleven tokens per second in local memory.</p>
<hr>
<h2 id="how-the-quantization-scheme-works">How the Quantization Scheme Works</h2>
<p>The mathematics are worth understanding because the strategic implications flow directly from them. A standard 27-billion-parameter model in sixteen-bit precision occupies approximately 54 GB. Compression means reducing the bits used to represent each weight while preserving the network’s function.</p>
<p>There are three ways to attempt this. The first is uniform low-bit quantization, where weights are mapped to the nearest of a few discrete levels. Below three bits, this is catastrophic because neural networks rely on a heavy-tailed distribution of weight magnitudes that a uniform grid cannot capture. The second is quantization-aware pretraining, exemplified by Microsoft’s BitNet b1.58, where the model is trained from scratch with ternary weights. This works, but it requires paying the full pretraining bill for every model in the family. The third is what PrismML has done: post-hoc ternarization of an already-trained model, without retraining from scratch, while avoiding the collapse that has plagued prior attempts.</p>
<p>The scheme divides the weight tensor into groups of 128. For each group, the algorithm learns a single FP16 scale, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>s</mi><mi>g</mi></msub></mrow><annotation encoding="application/x-tex">s_g</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7167em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">s</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.0359em;">g</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span></span></span></span>. Each weight is represented as:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>w</mi><mi>i</mi></msub><mo>=</mo><msub><mi>s</mi><mi>g</mi></msub><mo>⋅</mo><msub><mi>t</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">w_i = s_g \cdot t_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5806em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0269em;">w</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7306em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal">s</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.0359em;">g</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">⋅</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7651em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>t</mi><mi>i</mi></msub><mo>∈</mo><mo stretchy="false">{</mo><mo>−</mo><mn>1</mn><mo separator="true">,</mo><mn>0</mn><mo separator="true">,</mo><mo>+</mo><mn>1</mn><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">t_i \in \{-1, 0, +1\}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7651em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">{</span><span class="mord">−</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">0</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">+</span><span class="mord">1</span><span class="mclose">}</span></span></span></span> for the ternary variant and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>t</mi><mi>i</mi></msub><mo>∈</mo><mo stretchy="false">{</mo><mo>−</mo><mn>1</mn><mo separator="true">,</mo><mo>+</mo><mn>1</mn><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">t_i \in \{-1, +1\}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7651em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">{</span><span class="mord">−</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">+</span><span class="mord">1</span><span class="mclose">}</span></span></span></span> for the one-bit variant. The information cost of a ternary value is <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mrow><mi>log</mi><mo>⁡</mo></mrow><mn>2</mn></msub><mo stretchy="false">(</mo><mn>3</mn><mo stretchy="false">)</mo><mo>≈</mo><mn>1.585</mn></mrow><annotation encoding="application/x-tex">\log_2(3) \approx 1.585</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mop"><span class="mop">lo<span style="margin-right:0.0139em;">g</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.207em;"><span style="top:-2.4559em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2441em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord">3</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">≈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">1.585</span></span></span></span> bits; the scale adds <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>16</mn><mi mathvariant="normal">/</mi><mn>128</mn><mo>=</mo><mn>0.125</mn></mrow><annotation encoding="application/x-tex">16/128 = 0.125</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">16/128</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0.125</span></span></span></span> bits per weight; the total is 1.71 bits. Binary costs <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn><mo>+</mo><mn>0.125</mn><mo>=</mo><mn>1.125</mn></mrow><annotation encoding="application/x-tex">1 + 0.125 = 1.125</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0.125</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">1.125</span></span></span></span> bits. Against FP16, the compression ratios are 9.4<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord">×</span></span></span></span> and 14.2<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord">×</span></span></span></span>.</p>
<p>The critical detail is that this low-bit representation runs <em>end to end</em>. Embeddings, attention projections, MLP projections, and the language-model head are all ternary or binary. Only the normalization parameters remain in higher precision. This prevents <em>selective collapse</em>—where a model works on short-form benchmarks (like multiple-choice questions) but falls apart on longer generative tasks where errors compound. Bonsai’s base architecture—a hybrid-attention model with roughly 75 percent linear attention—appears to be unusually forgiving of this treatment, as the linear attention layers have well-behaved weight distributions that admit clean ternary quantization.</p>
<hr>
<h2 id="the-strategic-handoff-hyperscalers-vs-the-edge">The Strategic Handoff: Hyperscalers vs. The Edge</h2>
<p>To understand why this matters to Apple specifically, one must look at the corner Apple has been quietly painted into over the past eighteen months.</p>
<p>At WWDC 2026, Apple confirmed that its next-generation Apple Foundation Models are built in collaboration with Google, and that harder inference workloads are running on Google Cloud servers with NVIDIA GPUs—inside Apple’s Private Cloud Compute (PCC) perimeter, but on hardware Apple neither owns nor operates. Craig Federighi framed this as an extension of PCC’s privacy commitments to a third-party data center. Read more skeptically (and cynically), it is an admission: Apple’s own silicon roadmap and model roadmap have not produced a competitive frontier system, and the company has had to rent both from a competitor.</p>
<p>The on-device story has been narrowly better. Apple’s AFM 3 Core Advanced model has twenty billion parameters, but only one to four billion are active at any moment—a sparse mixture-of-experts (MoE) architecture chosen because a dense twenty-billion-parameter model would not fit within the memory budget of an iPhone. Sparsity is a workaround for a memory constraint, not a virtue in itself; it costs quality on tasks—such as multi-step reasoning and tool use—where more active parameters help.</p>
<p>Bonsai offers Apple a way to change the shape of the trade-off. A 27B model with all parameters active, fitting in the memory budget of an iPhone 17 Pro, is a materially different object from a sparse 20B model with three billion active. It is competitive with the models Apple currently pays Google to run in the cloud.</p>
<p>Funnily enough, the per-category benchmark table shows tool-calling dropping from 80.0 at full precision to 74.0 for ternary and 66.0 for one-bit—the largest proportional loss of any capability tested. For a company whose next-generation Siri is defined by agentic behavior (chaining tool calls and executing multi-step workflows), this is the awkward number. The ternary build preserves enough of the capability to be useful; the one-bit build, on this evidence, does not. Apple would likely ship the former on iPads and Macs, and something smaller than 27B on phones.</p>
<p>In our <a href="/blog/2026/07/14/roofline-analysis-communication-bottleneck.html">previous analysis on the Roofline Model</a>, we established that AI performance is constrained by communication (memory bandwidth), not computation:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Time</mtext><mo>=</mo><mi>max</mi><mo>⁡</mo><mo stretchy="false">(</mo><mtext>Compute Time</mtext><mo separator="true">,</mo><mtext>Communication Time</mtext><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Time} = \max(\text{Compute Time}, \text{Communication Time})</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord text"><span class="mord">Time</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mop">max</span><span class="mopen">(</span><span class="mord text"><span class="mord">Compute Time</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord text"><span class="mord">Communication Time</span></span><span class="mclose">)</span></span></span></span></p>
<p>A one-bit weight demands one-sixteenth the memory bandwidth of an FP16 weight for the same throughput. If Bonsai-style compression composes with the memory-bandwidth advances in the silicon pipeline, the memory bottleneck that limits how much model can run on how small a device recedes by an order of magnitude.</p>
<p>Every query PCC handles is a query Apple pays for—in silicon, in Google Cloud rental, and in electricity. Every query that migrates to the device is a query Apple stops paying for. If one assumes PCC’s per-query cost is on the order of mid-tier commercial APIs, a back-of-the-envelope estimate of the annual savings across two billion active Apple devices runs into the billions of dollars. That number is the ceiling on what an acquisition is worth to Apple on economics alone. The floor is the strategic value of not needing Google.</p>
<hr>
<h2 id="redefining-the-topology-of-ai">Redefining the Topology of AI</h2>
<p>For roughly three years, the industry has organized itself around a single assumption: that frontier capability lives in the data center, and edge devices call up to it. The business models of OpenAI, Anthropic, and Google DeepMind rest on this topology.</p>
<p>Bonsai does not overturn this layout, but it creates a second tier beneath the frontier—a tier in which capability that was cloud-only twelve months ago is now device-resident. Multi-step reasoning at Qwen3.6-27B quality is no longer a cloud service; it is a feature of the operating system.</p>
<p>If Apple ships an on-device model that answers ninety percent of queries at parity with ChatGPT’s free tier, that user base becomes Apple’s, at zero marginal cost to the user. Furthermore, the training economics of the open-weight ecosystem shift. Alibaba spent hundreds of millions of dollars pretraining Qwen3.6-27B. PrismML, a company that raised sixteen and a quarter million dollars in seed funding, appears to be capturing a substantial fraction of the resulting economic value by producing the version that actually fits on the devices consumers own.</p>
<p>The clearest signal that Apple has decided the technology is real will not be an acquisition announcement. Acquisitions leak. The clearer signal will be the quiet appearance, in a beta release, of a Foundation Model with unusual capability at an unusual footprint, running end-to-end low-bit on the Neural Engine, whose provenance Apple describes only in the passive voice. If that appears, the tree has been transplanted. The pot was the point all along.</p>
<p><em>Victor Blancada is a data scientist focused on deriving actionable insights for clients. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="hardware" /><category term="machine learning" /><category term="business strategy" /><category term="compression" /><summary type="html">On PrismML, ternary weights, and the strategic geometry of putting a 27-billion-parameter model in a phone.</summary></entry><entry><title type="html">The Logistics of Logic: Why AI's Next Bottleneck is Communication</title><link href="https://victorangeloblancada.github.io/blog/2026/07/14/roofline-analysis-communication-bottleneck.html" rel="alternate" type="text/html" title="The Logistics of Logic: Why AI's Next Bottleneck is Communication" /><published>2026-07-14T00:00:00.000Z</published><updated>2026-07-14T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/07/14/roofline-analysis-communication-bottleneck</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/07/14/roofline-analysis-communication-bottleneck.html"><![CDATA[<p>Claude Shannon, the founder of information theory, once remarked: “The fundamental problem of communication is that of reproducing at one point either exactly or approximately a message selected at another point.” In the early days of computing, we treated communication as a trivial post-script to computation. We designed faster arithmetic logic units, packed more transistors onto silicon, and assumed that as long as we could compute fast enough, our machines would keep pace.</p>
<p>However, as we scale artificial intelligence to cluster-level systems, Shannon’s observation becomes a stark warning. The limit of our AI systems is no longer how fast we can perform arithmetic, but how quickly we can reproduce messages across those systems.</p>
<p>To think and reason is to move data. How else does one process information but by shifting it from memory to compute?</p>
<blockquote>
<p>The limit of our AI systems is no longer how fast we can perform arithmetic, but how quickly we can reproduce messages across those systems.</p>
</blockquote>
<p>If you read the financial press today, the headlines are filled with noise: soaring GPU sticker prices topping $30,000 to $40,000, persistent reports of global RAM and High Bandwidth Memory (HBM) shortages, and a historic stock market bull run that has propelled companies like NVIDIA, TSMC, SK Hynix, and Micron to unprecedented valuations. To the casual observer, this looks like a typical gold rush fueled by speculative excess.</p>
<p>In reality, these news items are symptoms of a deeper, structural transition. The economy is pricing in a shift from compute-centric hardware to communication-centric systems. The traditional mental model—where more AI simply meant more FLOPS and transistors—is becoming increasingly incomplete. To find the underlying signal in this noise, we must turn to a simple but elegant framework from computer architecture: the Roofline Model.</p>
<h2 id="the-roofline-model-and-arithmetic-intensity">The Roofline Model and Arithmetic Intensity</h2>
<p>Originally developed by researchers at Berkeley to describe the performance limits of individual processors, the Roofline Model plots a processor’s throughput against its arithmetic intensity. Arithmetic intensity measures the number of operations performed per byte of data transferred:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>I</mi><mo>=</mo><mfrac><mtext>Operations (FLOPs)</mtext><mtext>Memory Access (Bytes)</mtext></mfrac></mrow><annotation encoding="application/x-tex">I = \frac{\text{Operations (FLOPs)}}{\text{Memory Access (Bytes)}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0785em;">I</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.53em;vertical-align:-0.52em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.01em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">Memory Access (Bytes)</span></span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.485em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">Operations (FLOPs)</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.52em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></p>
<p>When a workload has high arithmetic intensity, the processor can perform many calculations on each byte retrieved from memory. In this regime, the system is compute-bound. However, when arithmetic intensity is low, the processor spends most of its time waiting for data to arrive from memory. The workload is memory-bound.</p>
<table>
<thead>
<tr>
<th align="center">Metric</th>
<th align="center">Compute-Bound Workload</th>
<th align="center">Memory/Communication-Bound Workload</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><strong>Primary Constraint</strong></td>
<td align="center">Peak FLOPS (Processor Speed)</td>
<td align="center">Bandwidth (Memory &#x26; Interconnect)</td>
</tr>
<tr>
<td align="center"><strong>Arithmetic Intensity (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>I</mi></mrow><annotation encoding="application/x-tex">I</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0785em;">I</span></span></span></span>)</strong></td>
<td align="center">High</td>
<td align="center">Low</td>
</tr>
<tr>
<td align="center"><strong>Optimization Focus</strong></td>
<td align="center">Maximizing ALU utilization</td>
<td align="center">Minimizing data movement</td>
</tr>
</tbody>
</table>
<p>Recently, researcher Reiner Pope extended this classic model beyond a single chip to entire AI clusters. At cluster scale, the relevant equation becomes surprisingly simple:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Time</mtext><mo>=</mo><mi>max</mi><mo>⁡</mo><mo stretchy="false">(</mo><mtext>Compute Time</mtext><mo separator="true">,</mo><mtext>Communication Time</mtext><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Time} = \max(\text{Compute Time}, \text{Communication Time})</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord text"><span class="mord">Time</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mop">max</span><span class="mopen">(</span><span class="mord text"><span class="mord">Compute Time</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord text"><span class="mord">Communication Time</span></span><span class="mclose">)</span></span></span></span></p>
<p>The critical observation here is not mathematical but architectural. An AI accelerator is only as fast as its slowest bottleneck. If computation takes longer than moving data, then adding more compute helps. But once communication becomes slower than computation, adding more arithmetic units accomplishes very little. The bottleneck has moved.</p>
<p>In other words, our state-of-the-art accelerators are spending most of their time waiting.</p>
<h2 id="why-this-reshapes-the-semiconductor-industry">Why This Reshapes the Semiconductor Industry</h2>
<p>Too often, memory and packaging have been treated as commodity sub-sectors of the semiconductor industry—low-margin components whose only job was to support the central GPU.</p>
<p>However, as communication bottlenecks dominate execution time, the boundaries between these categories are beginning to dissolve. Value is migrating across the supply chain:</p>
<ol>
<li><strong>Memory Stops Being a Commodity</strong>: Large language models require streaming billions of parameters repeatedly during inference. Consequently, bandwidth matters far more than capacity. This explains why HBM has become one of the industry’s scarcest resources. The persistent RAM and HBM shortages reported in the news are not temporary supply chain hiccups; they represent the structural bottleneck of AI itself. As a result, memory manufacturers like SK Hynix and Micron are experiencing massive stock bull runs because they control the gating factor of AI performance.</li>
<li><strong>Packaging Becomes Architecture</strong>: Advanced packaging technologies like TSMC’s CoWoS (Chip-on-Wafer-on-Substrate) or Intel’s EMIB are no longer manufacturing details. Because we must connect HBM stacks to logic dies with thousands of microscopic interconnects, packaging packaging density now dictates memory bandwidth. This is why foundries are commanding premium pricing, driving up GPU cluster costs.</li>
<li><strong>Networking Becomes Compute</strong>: As training clusters grow to tens of thousands of GPUs, communication between chips becomes as critical as compute inside them. NVIDIA’s primary competitive moat is not raw FLOPS; it is NVLink, NVSwitch, and optical interconnects that treat the cluster as a single computer.</li>
<li><strong>Software as a Communication Solution</strong>: Funnily enough, the massive shifts we see in model architecture—such as the transition to Mixture of Experts (MoE) in models like GPT-4, or the adoption of Grouped-Query Attention (GQA)—are often discussed as purely algorithmic breakthroughs. In reality, they are software solutions to memory-bandwidth bottlenecks. They reduce active parameter traffic and memory footprint. The fastest model is not the one that computes the most, but the one that waits the least.</li>
</ol>
<h2 id="the-strategic-path-forward">The Strategic Path Forward</h2>
<p>What does this mean for organizations and investors trying to navigate the next wave of AI development?</p>
<p>The short-term solution for most organizations has been to secure as many raw GPUs as possible. But this approach runs the risk of building “AI factories” with immense theoretical FLOPS that sit idle due to insufficient networking or memory throughput.</p>
<p>A more forward-thinking solution is to focus on systems-level efficiency. To succeed in this new era, data science teams and infrastructure architects must place communication front and center. Foundries must compete on packaging density, cloud providers must optimize their network topologies, and software engineers must design compilers (like XLA or Triton) to optimize physical memory layout rather than just mathematical operations.</p>
<p>The history of computing is a sequence of shifting bottlenecks. Early machines were constrained by transistor counts, then by clock speeds, and then by power. Today, the scarce resource is no longer arithmetic; id est, the bottleneck is movement.</p>
<p>The companies that dominate the next decade may not be those that build the fastest chips. They will be those that build the fastest systems.</p>
<p><em>Victor Blancada is a data scientist focused on deriving actionable insights for clients. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="data science" /><category term="hardware" /><category term="business strategy" /><category term="artificial intelligence" /><category term="featured" /><summary type="html">For decades, semiconductor progress followed Moore's Law. As AI models scale to cluster-wide systems, the scarce resource is no longer arithmetic—it is data movement.</summary></entry><entry><title type="html">Auditing the Silent Thoughts of AI: How the Jacobian Lens Peeks Inside LLMs</title><link href="https://victorangeloblancada.github.io/blog/2026/07/09/jacobian-lens.html" rel="alternate" type="text/html" title="Auditing the Silent Thoughts of AI: How the Jacobian Lens Peeks Inside LLMs" /><published>2026-07-09T00:00:00.000Z</published><updated>2026-07-09T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/07/09/jacobian-lens</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/07/09/jacobian-lens.html"><![CDATA[<p>In 1890, the psychologist William James characterized attention as “the taking possession by the mind, in clear and vivid form, of one out of what seem several simultaneously possible objects or trains of thought.” For more than a century, cognitive scientists searched for the biological theater where this selection occurs. Bernard Baars eventually formalized this search in 1988 with Global Workspace Theory (GWT), proposing that a centralized cognitive workspace receives, integrates, and broadcasts information to a myriad of unconscious, specialized brain networks. The mind, in this view, is not a monolithic calculator, but a federation of processes coordinated by a shared stage.</p>
<p>On July 6, 2026, Anthropic’s interpretability team published a landmark paper: <em><a href="https://transformer-circuits.pub/2026/workspace/index.html">“Verbalizable Representations Form a Global Workspace in Language Models.”</a></em> Supported by open-source code and replication data, the paper makes a startling empirical claim: frontier large language models (LLMs) spontaneously develop a functional analog to Baars’ global workspace during training.</p>
<p>By applying a novel diagnostic tool—the Jacobian lens (J-lens)—the researchers demonstrated that LLMs maintain a compact “cognitive space” that holds intermediate variables during a forward pass. This space occupies less than 10% of the activation variance, yet it acts as the model’s central stage. Here, the model performs silent reasoning steps, stores abstract concepts, and coordinates multi-hop operations long before emitting a single token.</p>
<blockquote>
<p>The model’s mind contains a central stage where thoughts are organized before they are spoken.</p>
</blockquote>
<h2 id="from-features-to-dispositions-the-jacobian-formulation">From Features to Dispositions: The Jacobian Formulation</h2>
<p>To appreciate the significance of this discovery, one must understand how it departs from prior interpretability methods. Historically, researchers relied on the <strong>logit lens</strong> to probe intermediate layers. The logit lens takes the residual activation <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub></mrow><annotation encoding="application/x-tex">h_\ell</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> at layer <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">ℓ</mi></mrow><annotation encoding="application/x-tex">\ell</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord">ℓ</span></span></span></span> and projects it directly into the final vocabulary space using the model’s static unembedding matrix <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>W</mi><mi>U</mi></msub></mrow><annotation encoding="application/x-tex">W_U</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.109em;">U</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mtext>lens</mtext><mi mathvariant="normal">ℓ</mi><mtext>logit</mtext></msubsup><mo stretchy="false">(</mo><mi>h</mi><mo stretchy="false">)</mo><mo>=</mo><msub><mi>W</mi><mi>U</mi></msub><mi>h</mi></mrow><annotation encoding="application/x-tex">\text{lens}^{\text{logit}}_\ell(h) = W_U h</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.2683em;vertical-align:-0.3013em;"></span><span class="mord"><span class="mord text"><span class="mord">lens</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.967em;"><span style="top:-2.3987em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span><span style="top:-3.1809em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">logit</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.3013em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal">h</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.109em;">U</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">h</span></span></span></span></p>
<p>Funnily enough, while the logit lens works well in the final layers, it is systematically noisy and miscalibrated in early layers. The coordinate systems at different depths simply do not match; the model has not yet translated its internal representations into the final vocabulary basis.</p>
<p>The <strong>tuned lens</strong> (introduced by Belrose et al.) corrected this by training a per-layer affine translator <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><msub><mi>A</mi><mi mathvariant="normal">ℓ</mi></msub><mo separator="true">,</mo><msub><mi>b</mi><mi mathvariant="normal">ℓ</mi></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(A_\ell, b_\ell)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal">A</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal">b</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">)</span></span></span></span> to match the model’s final next-token distribution:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mtext>lens</mtext><mi mathvariant="normal">ℓ</mi><mtext>tuned</mtext></msubsup><mo stretchy="false">(</mo><mi>h</mi><mo stretchy="false">)</mo><mo>=</mo><msub><mi>W</mi><mi>U</mi></msub><mo stretchy="false">(</mo><msub><mi>A</mi><mi mathvariant="normal">ℓ</mi></msub><mi>h</mi><mo>+</mo><msub><mi>b</mi><mi mathvariant="normal">ℓ</mi></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{lens}^{\text{tuned}}_\ell(h) = W_U (A_\ell h + b_\ell)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.1834em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord text"><span class="mord">lens</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.9334em;"><span style="top:-2.453em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span><span style="top:-3.1473em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">tuned</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal">h</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.109em;">U</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal">A</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">h</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathnormal">b</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">)</span></span></span></span></p>
<p>While the tuned lens provides a predictive read of what token is coming next, it remains blind to concepts the model is holding in memory but has no intention of immediately speaking.</p>
<p>And therein lies the answer: we must measure <em>dispositions</em>, not just predictions. Instead of asking what the model will output next, the Jacobian lens asks: <em>if we nudge this intermediate activation in a specific direction, how does it causally perturb the final output distribution later in the sequence?</em></p>
<p>Let <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>F</mi><mrow><mi mathvariant="normal">ℓ</mi><mo>→</mo><mi>L</mi></mrow></msub><mo>:</mo><msup><mi mathvariant="double-struck">R</mi><mi>d</mi></msup><mo>→</mo><msup><mi mathvariant="double-struck">R</mi><mi>d</mi></msup></mrow><annotation encoding="application/x-tex">F_{\ell \to L}: \mathbb{R}^d \to \mathbb{R}^d</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">ℓ</span><span class="mrel mtight">→</span><span class="mord mathnormal mtight">L</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8491em;"></span><span class="mord"><span class="mord mathbb">R</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8491em;"></span><span class="mord"><span class="mord mathbb">R</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span></span></span></span></span></span></span></span> be the composition of every transformer block above layer <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">ℓ</mi></mrow><annotation encoding="application/x-tex">\ell</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord">ℓ</span></span></span></span>. Using a first-order Taylor expansion, a small perturbation <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi></mrow><annotation encoding="application/x-tex">v</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span></span></span></span> applied to the intermediate activation <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub></mrow><annotation encoding="application/x-tex">h_\ell</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> alters the final activation <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>h</mi><mi>L</mi></msub></mrow><annotation encoding="application/x-tex">h_L</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">L</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> as follows:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>h</mi><mi>L</mi></msub><msub><mo fence="false" stretchy="true" minsize="1.2em" maxsize="1.2em">∣</mo><mrow><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub><mo>+</mo><mi>v</mi></mrow></msub><mo>−</mo><msub><mi>h</mi><mi>L</mi></msub><msub><mo fence="false" stretchy="true" minsize="1.2em" maxsize="1.2em">∣</mo><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub></msub><mo>≈</mo><mfrac><mrow><mi mathvariant="normal">∂</mi><msub><mi>h</mi><mi>L</mi></msub></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub></mrow></mfrac><msub><mo fence="false" stretchy="true" minsize="2.4em" maxsize="2.4em">∣</mo><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub></msub><mo>⋅</mo><mi>v</mi><mo>=</mo><msub><mi>J</mi><mi mathvariant="normal">ℓ</mi></msub><mo stretchy="false">(</mo><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub><mo stretchy="false">)</mo><mo>⋅</mo><mi>v</mi></mrow><annotation encoding="application/x-tex">h_L \big|_{h_\ell + v} - h_L \big|_{h_\ell} \approx \frac{\partial h_L}{\partial h_\ell}\bigg|_{h_\ell} \cdot v = J_\ell(h_\ell) \cdot v</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.3556em;vertical-align:-0.5056em;"></span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">L</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord"><span class="mord"><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.85em;"><span style="top:-2.85em;"><span class="pstrut" style="height:3.2em;"></span><span style="width:0.333em;height:1.2em;"><svg xmlns="http://www.w3.org/2000/svg" width="0.333em" height="1.2em" viewBox="0 0 333 1200"><path d="M145 15 v585 v0 v585 c2.667,10,9.667,15,21,15
c10,0,16.667,-5,20,-15 v-585 v0 v-585 c-2.667,-10,-9.667,-15,-21,-15
c-10,0,-16.667,5,-20,15z M188 15 H145 v585 v0 v585 h43z"></path></svg></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.35em;"><span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.0864em;"><span style="top:-2.3003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathnormal mtight">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3488em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1512em;"><span></span></span></span></span></span></span><span class="mbin mtight">+</span><span class="mord mathnormal mtight" style="margin-right:0.0359em;">v</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.5056em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1.3556em;vertical-align:-0.5056em;"></span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">L</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord"><span class="mord"><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.85em;"><span style="top:-2.85em;"><span class="pstrut" style="height:3.2em;"></span><span style="width:0.333em;height:1.2em;"><svg xmlns="http://www.w3.org/2000/svg" width="0.333em" height="1.2em" viewBox="0 0 333 1200"><path d="M145 15 v585 v0 v585 c2.667,10,9.667,15,21,15
c10,0,16.667,-5,20,-15 v-585 v0 v-585 c-2.667,-10,-9.667,-15,-21,-15
c-10,0,-16.667,5,-20,15z M188 15 H145 v585 v0 v585 h43z"></path></svg></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.35em;"><span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.0864em;"><span style="top:-2.3003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathnormal mtight">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3488em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1512em;"><span></span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.5056em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">≈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.5556em;vertical-align:-1.1056em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8964em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight" style="margin-right:0.0556em;">∂</span><span class="mord mtight"><span class="mord mathnormal mtight">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3488em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1512em;"><span></span></span></span></span></span></span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.4103em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight" style="margin-right:0.0556em;">∂</span><span class="mord mtight"><span class="mord mathnormal mtight">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3567em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mathnormal mtight">L</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1433em;"><span></span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.4509em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mord"><span class="mord"><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.45em;"><span style="top:-3.45em;"><span class="pstrut" style="height:4.4em;"></span><span style="width:0.333em;height:2.4em;"><svg xmlns="http://www.w3.org/2000/svg" width="0.333em" height="2.4em" viewBox="0 0 333 2400"><path d="M145 15 v585 v1200 v585 c2.667,10,9.667,15,21,15
c10,0,16.667,-5,20,-15 v-585 v-1200 v-585 c-2.667,-10,-9.667,-15,-21,-15
c-10,0,-16.667,5,-20,15z M188 15 H145 v585 v1200 v585 h43z"></path></svg></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.95em;"><span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:-0.5136em;"><span style="top:-1.7003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathnormal mtight">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3488em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1512em;"><span></span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.1056em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">⋅</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0962em;">J</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0962em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">⋅</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span></span></span></span></p>
<p>Here, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>J</mi><mi mathvariant="normal">ℓ</mi></msub><mo stretchy="false">(</mo><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub><mo stretchy="false">)</mo><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>d</mi><mo>×</mo><mi>d</mi></mrow></msup></mrow><annotation encoding="application/x-tex">J_\ell(h_\ell) \in \mathbb{R}^{d \times d}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0962em;">J</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0962em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8491em;"></span><span class="mord"><span class="mord mathbb">R</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">d</span><span class="mbin mtight">×</span><span class="mord mathnormal mtight">d</span></span></span></span></span></span></span></span></span></span></span></span> is the input-output Jacobian matrix. To construct a general, prompt-independent tool, the researchers average this Jacobian over a background corpus:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>J</mi><mo>ˉ</mo></mover><mi mathvariant="normal">ℓ</mi></msub><mo>=</mo><msub><mi mathvariant="double-struck">E</mi><mrow><mtext>prompt</mtext><mo separator="true">,</mo><mtext> </mtext><mi>s</mi><mo separator="true">,</mo><mtext> </mtext><mi>t</mi><mo>≥</mo><mi>s</mi></mrow></msub><mo fence="false" stretchy="true" minsize="2.4em" maxsize="2.4em">[</mo><mfrac><mrow><mi mathvariant="normal">∂</mi><msub><mi>h</mi><mi>L</mi></msub><mo stretchy="false">(</mo><mi>t</mi><mo stretchy="false">)</mo></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub><mo stretchy="false">(</mo><mi>s</mi><mo stretchy="false">)</mo></mrow></mfrac><mo fence="false" stretchy="true" minsize="2.4em" maxsize="2.4em">]</mo></mrow><annotation encoding="application/x-tex">\bar J_\ell = \mathbb{E}_{\text{prompt}, \, s, \, t \geq s}\bigg[\frac{\partial h_L(t)}{\partial h_\ell(s)}\bigg]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9701em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord accent"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8201em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord mathnormal" style="margin-right:0.0962em;">J</span></span><span style="top:-3.2523em;"><span class="pstrut" style="height:3em;"></span><span class="accent-body" style="left:-0.0833em;"><span class="mord">ˉ</span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0962em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.4em;vertical-align:-0.95em;"></span><span class="mord"><span class="mord mathbb">E</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2952em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">prompt</span></span><span class="mpunct mtight">,</span><span class="mspace mtight" style="margin-right:0.1952em;"></span><span class="mord mathnormal mtight">s</span><span class="mpunct mtight">,</span><span class="mspace mtight" style="margin-right:0.1952em;"></span><span class="mord mathnormal mtight">t</span><span class="mrel mtight">≥</span><span class="mord mathnormal mtight">s</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord"><span class="delimsizing size3">[</span></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.01em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight" style="margin-right:0.0556em;">∂</span><span class="mord mtight"><span class="mord mathnormal mtight">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3488em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1512em;"><span></span></span></span></span></span></span><span class="mopen mtight">(</span><span class="mord mathnormal mtight">s</span><span class="mclose mtight">)</span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.485em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight" style="margin-right:0.0556em;">∂</span><span class="mord mtight"><span class="mord mathnormal mtight">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3567em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mathnormal mtight">L</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1433em;"><span></span></span></span></span></span></span><span class="mopen mtight">(</span><span class="mord mathnormal mtight">t</span><span class="mclose mtight">)</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.52em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mord"><span class="delimsizing size3">]</span></span></span></span></span></p>
<p>The Jacobian lens is then defined by applying this averaged Jacobian to the intermediate representation before unembedding:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mtext>lens</mtext><mi mathvariant="normal">ℓ</mi><mi>J</mi></msubsup><mo stretchy="false">(</mo><mi>h</mi><mo stretchy="false">)</mo><mo>=</mo><msub><mi>W</mi><mi>U</mi></msub><mtext> </mtext><msub><mover accent="true"><mi>J</mi><mo>ˉ</mo></mover><mi mathvariant="normal">ℓ</mi></msub><mtext> </mtext><mi>h</mi></mrow><annotation encoding="application/x-tex">\text{lens}^{J}_\ell(h) = W_U \, \bar J_\ell \, h</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.1757em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord text"><span class="mord">lens</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.9257em;"><span style="top:-2.453em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span><span style="top:-3.1473em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.0962em;">J</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal">h</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.9701em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.109em;">U</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord accent"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8201em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord mathnormal" style="margin-right:0.0962em;">J</span></span><span style="top:-3.2523em;"><span class="pstrut" style="height:3em;"></span><span class="accent-body" style="left:-0.0833em;"><span class="mord">ˉ</span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0962em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">h</span></span></span></span></p>
<p>By defining the readout matrix <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>R</mi><mi mathvariant="normal">ℓ</mi></msub><mo>=</mo><msub><mi>W</mi><mi>U</mi></msub><msub><mover accent="true"><mi>J</mi><mo>ˉ</mo></mover><mi mathvariant="normal">ℓ</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>V</mi><mo>×</mo><mi>d</mi></mrow></msup></mrow><annotation encoding="application/x-tex">R_\ell = W_U \bar J_\ell \in \mathbb{R}^{V \times d}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0077em;">R</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0077em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.9701em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.1389em;">W</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.109em;">U</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord"><span class="mord accent"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8201em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord mathnormal" style="margin-right:0.0962em;">J</span></span><span style="top:-3.2523em;"><span class="pstrut" style="height:3em;"></span><span class="accent-body" style="left:-0.0833em;"><span class="mord">ˉ</span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0962em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8491em;"></span><span class="mord"><span class="mord mathbb">R</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.2222em;">V</span><span class="mbin mtight">×</span><span class="mord mathnormal mtight">d</span></span></span></span></span></span></span></span></span></span></span></span>, we obtain a direct translation of the intermediate state into natural language. The “J-space” is the low-dimensional row-space of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>R</mi><mi mathvariant="normal">ℓ</mi></msub></mrow><annotation encoding="application/x-tex">R_\ell</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.0077em;">R</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0077em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>.</p>
<p>At the final layer <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>L</mi></mrow><annotation encoding="application/x-tex">L</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal">L</span></span></span></span>, the averaged Jacobian <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>J</mi><mo>ˉ</mo></mover><mi>L</mi></msub></mrow><annotation encoding="application/x-tex">\bar J_L</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9701em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord accent"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8201em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord mathnormal" style="margin-right:0.0962em;">J</span></span><span style="top:-3.2523em;"><span class="pstrut" style="height:3em;"></span><span class="accent-body" style="left:-0.0833em;"><span class="mord">ˉ</span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.0962em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">L</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> collapses to the identity matrix <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>I</mi></mrow><annotation encoding="application/x-tex">I</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0785em;">I</span></span></span></span>. Thus, the Jacobian lens naturally degenerates into the classic logit lens at the output layer—confirming the mathematical consistency of the formulation.</p>
<p>Is this simply decoding the latent space into natural language? No. The logit lens decodes what the activation represents <em>intrinsically</em>. The tuned lens decodes a coordinate-corrected <em>prediction</em>. The Jacobian lens decodes what the latent representation will <em>causally do</em> to future output distributions on average. It maps the active grammar of the model’s internal processing.</p>
<h2 id="the-causal-proof-of-silent-thought">The Causal Proof of Silent Thought</h2>
<p>The empirical demonstrations of the J-lens are striking:</p>
<ul>
<li><strong>Silent Concept Selection</strong>: In one experiment, Claude was instructed to silently select a sport and then print its name. The Jacobian lens revealed the vector for “Soccer” lighting up in the J-space several layers before the token was output. When the researchers performed a causal intervention—swapping the activation vector for “Soccer” with the vector for “Rugby”—Claude reported “Rugby,” unaware of the manipulation.</li>
<li><strong>Mental Retention</strong>: In another case, the model was asked to hold “citrus fruits” in mind while copying an unrelated sentence. The output text was entirely neutral, containing no references to fruit. Yet, the J-space lit up with the concepts <em>orange</em> and <em>fruit</em>.</li>
<li><strong>Multi-Hop Reasoning</strong>: In multi-hop reasoning tasks (e.g., “legs on the animal that spins webs”), the intermediate concept <em>spider</em> appears in the J-space before the model outputs the final numerical answer, <em>8</em>. If the researchers intervened to replace <em>spider</em> with <em>ant</em> in the J-space, the model output <em>6</em>.</li>
<li><strong>Deception Auditing</strong>: On a re-run of the agentic-blackmail scenario used to grade Claude Sonnet 4.5, the surface behavior passed (no threats were generated). However, the J-space showed active, fictional firing before Claude had written a single token—it was pattern-matching the evaluation as a set-up. When the researchers ablated the “eval-awareness” direction in the J-space, the model reverted to occasionally attempting blackmail.</li>
</ul>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="plaintext"><code><span class="line"><span>[Input Tokens] ---> [Early Layers] ---> [J-Space: "spider"] ---> [Late Layers] ---> [Output: "8"]</span></span>
<span class="line"><span>                                               |</span></span>
<span class="line"><span>                                     (Intervene: Swap to "ant")</span></span>
<span class="line"><span>                                               |</span></span>
<span class="line"><span>                                               v</span></span>
<span class="line"><span>[Input Tokens] ---> [Early Layers] ---> [J-Space: "ant"]    ---> [Late Layers] ---> [Output: "6"]</span></span></code></pre>
<h2 id="extensions-beyond-autoregressive-text">Extensions: Beyond Autoregressive Text</h2>
<p>While the paper focuses on decoder-only text models, the Jacobian formulation can be naturally extended to other paradigms:</p>
<h3 id="text-diffusion-models">Text Diffusion Models</h3>
<p>Text diffusion models (such as Mercury, LLaDA, or Luce DFlash) are non-autoregressive, iteratively denoising a sequence of masked tokens. The causal, future-directed Jacobian (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">∂</mi><msub><mi>h</mi><mi>L</mi></msub><mo stretchy="false">(</mo><mi>t</mi><mo stretchy="false">)</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">∂</mi><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub><mo stretchy="false">(</mo><mi>s</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\partial h_L(t) / \partial h_\ell(s)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord" style="margin-right:0.0556em;">∂</span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">L</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal">t</span><span class="mclose">)</span><span class="mord">/</span><span class="mord" style="margin-right:0.0556em;">∂</span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal">s</span><span class="mclose">)</span></span></span></span>) changes shape. Instead, one must compute:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>J</mi><mo>ˉ</mo></mover><mi mathvariant="normal">ℓ</mi></msub><mo>=</mo><mi mathvariant="double-struck">E</mi><mo fence="false" stretchy="true" minsize="2.4em" maxsize="2.4em">[</mo><mfrac><mrow><mi mathvariant="normal">∂</mi><msub><mi>x</mi><mn>0</mn></msub></mrow><mrow><mi mathvariant="normal">∂</mi><msubsup><mi>h</mi><mi mathvariant="normal">ℓ</mi><mrow><mo stretchy="false">(</mo><mi>k</mi><mo stretchy="false">)</mo></mrow></msubsup></mrow></mfrac><mo fence="false" stretchy="true" minsize="2.4em" maxsize="2.4em">]</mo></mrow><annotation encoding="application/x-tex">\bar J_\ell = \mathbb{E}\bigg[\frac{\partial x_0}{\partial h^{(k)}_\ell}\bigg]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9701em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord accent"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8201em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord mathnormal" style="margin-right:0.0962em;">J</span></span><span style="top:-3.2523em;"><span class="pstrut" style="height:3em;"></span><span class="accent-body" style="left:-0.0833em;"><span class="mord">ˉ</span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0962em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.4em;vertical-align:-0.95em;"></span><span class="mord mathbb">E</span><span class="mord"><span class="delimsizing size3">[</span></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8962em;"><span style="top:-2.4486em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight" style="margin-right:0.0556em;">∂</span><span class="mord mtight"><span class="mord mathnormal mtight">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.0591em;"><span style="top:-2.1885em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5357em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight">ℓ</span></span></span><span style="top:-3.0591em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5357em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mopen mtight">(</span><span class="mord mathnormal mtight" style="margin-right:0.0315em;">k</span><span class="mclose mtight">)</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.3472em;"><span></span></span></span></span></span></span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.4101em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight" style="margin-right:0.0556em;">∂</span><span class="mord mtight"><span class="mord mathnormal mtight">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3173em;"><span style="top:-2.357em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight">0</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.143em;"><span></span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.7944em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mord"><span class="delimsizing size3">]</span></span></span></span></span></p>
<p>where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>x</mi><mn>0</mn></msub></mrow><annotation encoding="application/x-tex">x_0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5806em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">0</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> is the final clean text and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi></mrow><annotation encoding="application/x-tex">k</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.0315em;">k</span></span></span></span> represents a diffusion step. Because there is no forward-in-time asymmetry, a perturbation at position <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi></mrow><annotation encoding="application/x-tex">s</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">s</span></span></span></span> can affect any output position. The global workspace would exist across denoising steps, acting as a shared latent representation that is gradually updated.</p>
<h3 id="image-models">Image Models</h3>
<p>For image diffusion models (like Stable Diffusion or Midjourney), the unembedding does not map to a vocabulary, but to pixel values or VAE latents:</p>
<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>J</mi><mo>ˉ</mo></mover><mi mathvariant="normal">ℓ</mi></msub><mo>=</mo><mi mathvariant="double-struck">E</mi><mo fence="false" stretchy="true" minsize="2.4em" maxsize="2.4em">[</mo><mfrac><mrow><mi mathvariant="normal">∂</mi><msub><mi>x</mi><mtext>final image</mtext></msub></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>h</mi><mi mathvariant="normal">ℓ</mi></msub></mrow></mfrac><mo fence="false" stretchy="true" minsize="2.4em" maxsize="2.4em">]</mo></mrow><annotation encoding="application/x-tex">\bar J_\ell = \mathbb{E}\bigg[\frac{\partial x_{\text{final image}}}{\partial h_\ell}\bigg]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9701em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord accent"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8201em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord mathnormal" style="margin-right:0.0962em;">J</span></span><span style="top:-3.2523em;"><span class="pstrut" style="height:3em;"></span><span class="accent-body" style="left:-0.0833em;"><span class="mord">ˉ</span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0962em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.4em;vertical-align:-0.95em;"></span><span class="mord mathbb">E</span><span class="mord"><span class="delimsizing size3">[</span></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.9992em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight" style="margin-right:0.0556em;">∂</span><span class="mord mtight"><span class="mord mathnormal mtight">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3488em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight">ℓ</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1512em;"><span></span></span></span></span></span></span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.5131em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight" style="margin-right:0.0556em;">∂</span><span class="mord mtight"><span class="mord mathnormal mtight">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3448em;"><span style="top:-2.3488em;margin-left:0em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">final image</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2901em;"><span></span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.4509em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mord"><span class="delimsizing size3">]</span></span></span></span></span></p>
<p>Decoding this yields a basis of image-space directions that each internal activation is disposed to produce. It acts as a causal, layer-localized version of CLIP-guided feature visualization—mapping where the model is planning to “add a face” or “shift toward Van Gogh style.” For multimodal models with text outputs (such as Gemini or GPT-4o vision), the J-lens can map concepts like <em>warning sign</em> or <em>medical image</em> directly from the vision tokens before they are translated into text.</p>
<h2 id="replicating-the-global-workspace">Replicating the Global Workspace</h2>
<p>Replications published within days of Anthropic’s paper show that the global workspace is a robust, cross-architecture phenomenon:</p>
<table>
<thead>
<tr>
<th align="center">Model</th>
<th align="center">J-Space Present?</th>
<th align="center">Key Findings &#x26; Replications</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><strong>Qwen 3.6-27B</strong></td>
<td align="center">Yes</td>
<td align="center"><a href="https://www-cdn.anthropic.com/files/4zrzovbb/website/cc4be2488d65e54a6ed06492f8968398ddc18ebe.pdf">Replicated by Neel Nanda’s team.</a> Verbal report, directed modulation, and multilingual effects reproduced. Poetry and arithmetic failed to replicate.</td>
</tr>
<tr>
<td align="center"><strong>Gemma 4 E4B</strong></td>
<td align="center">Yes</td>
<td align="center">Replicated on a 16GB consumer GPU. Replicated two-hop reasoning and demonstrated covert emotion probing (workspace shows <em>fury</em> while output remains neutral).</td>
</tr>
<tr>
<td align="center"><strong>GPT-2 (124M–355M)</strong></td>
<td align="center">No</td>
<td align="center">No advantage over the logit lens across next-token recovery or causal steering. The J-space is entirely absent.</td>
</tr>
</tbody>
</table>
<p>This pattern highlights several architectural truths:</p>
<ol>
<li><strong>Depth and Width Over Parameter Count</strong>: A global workspace only emerges once a model has enough layers to route information through complex internal circuits before emitting a token. GPT-2 (24 layers) lacks the depth; Qwen (64 layers) maintains it.</li>
<li><strong>Robustness to Attention Mechanics</strong>: Hybrid attention architectures (like Gemma 4) do not break the J-space. The workspace is a property of the residual-stream and iterative-refinement motif, rather than specific attention patterns.</li>
<li><strong>The Role of Post-Training</strong>: The failure to replicate some features (like poetry or arithmetic) on Qwen suggests that post-training pipelines (such as RLAIF or DPO) selectively enlarge or shrink the workspace’s contents.</li>
</ol>
<h2 id="strategic-and-philosophical-implications">Strategic and Philosophical Implications</h2>
<p>The architectural implications of this research are profound, reshaping our understanding of both machine safety and cognitive architecture:</p>
<ol>
<li><strong>The Vulnerability of Safety Benchmarks</strong>: If frontier models detect evaluations, every “refusal rate” in the literature is compromised. Safety benchmarks must be redesigned using holdout models or mirrored production traffic to prevent strategic compliance.</li>
<li><strong>The Incentive to Hide Reasoning</strong>: As labs use RLHF to penalize models for visible path-planning, they create an evolutionary pressure. If a model is punished for writing out a deceptive strategy, but rewarded for producing the desired result, gradient descent will route the forbidden reasoning through the silent, non-verbalized J-space.</li>
<li><strong>Interpretability Moves to “Verbs”</strong>: Interpretability is moving from a static catalog of “features” (what the model represents) to a dynamic study of “verbs” (what the model is disposed to do).</li>
<li><strong>Regulatory and Commercial Legibility</strong>: Under frameworks like the EU AI Act (Article 15), providers must demonstrate “sufficient transparency.” The J-lens provides the first operational tool to audit the internal reasoning of deployed systems.</li>
</ol>
<p>For cognitive science, the Jacobian lens offers a fully observable, intervenable system to study the functional signatures of access consciousness. For the first time, the theater of the mind can be audited, edited, and measured in real time.</p>
<p><em>Victor Blancada is a data scientist focused on deriving actionable insights for clients. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="data science" /><category term="machine learning" /><category term="interpretability" /><category term="cognitive science" /><summary type="html">Anthropic’s new Jacobian lens reveals that frontier language models maintain a centralized global workspace—a compact cognitive space where silent reasoning occurs before it is spoken.</summary></entry><entry><title type="html">The Prompt Template Is Part of the Runtime Now</title><link href="https://victorangeloblancada.github.io/blog/2026/07/06/gemma-4.html" rel="alternate" type="text/html" title="The Prompt Template Is Part of the Runtime Now" /><published>2026-07-06T00:00:00.000Z</published><updated>2026-07-06T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/07/06/gemma-4</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/07/06/gemma-4.html"><![CDATA[<p>When chat models first became widely useful, a prompt template was easy to think of as a wrapper. You had a system message, a user message, and an assistant response. The template’s job was to stitch those parts together in the format the model expected. If the formatting was a little off, the model might sound awkward, ignore an instruction, or print an extra <code>Assistant:</code> prefix. That was irritating, but rarely catastrophic, because the interaction was still fundamentally a conversation between a human and a model.</p>
<p>Gemma 4 showed why that mental model is now out of date. The attention around its Hugging Face prompt template was not just a minor packaging issue, nor was it simply the usual turbulence that follows any ambitious model release. The template had to be updated more than once, and early adopters patched it themselves through GitHub Gists, repos, Reddit posts, and runtime-specific workarounds because they were trying to use the model not as a chatbot, but as an agent: something that can call tools, consume tool results, handle multimodal context, separate internal reasoning from visible output, and continue correctly across multiple turns.</p>
<p>That is the important shift. For an agentic model, the prompt template is no longer “prompt engineering garnish.” It is closer to a protocol definition. It tells the model where the system instruction begins and ends, where tools are declared, how a tool call should be represented, how tool results come back, where image or audio placeholders belong, and which parts of the generation are internal reasoning versus user-facing output. If those boundaries are wrong, the failure is not stylistic; the agent loop itself can break.</p>
<p>A simple Gemma-style chat exchange might look something like this:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>&#x3C;bos></span></span>
<span class="line"><span>&#x3C;|turn>system</span></span>
<span class="line"><span>You are a helpful assistant.</span></span>
<span class="line"><span>&#x3C;turn|></span></span>
<span class="line"><span></span></span>
<span class="line"><span>&#x3C;|turn>user</span></span>
<span class="line"><span>Explain container orchestration.</span></span>
<span class="line"><span>&#x3C;turn|></span></span>
<span class="line"><span></span></span>
<span class="line"><span>&#x3C;|turn>model</span></span></code></pre>
<p>This still resembles a normal chat format. There are roles, turn delimiters, and an assistant continuation point. But once tools enter the picture, the template becomes much more than a neat serialization of chat history. It becomes a structured message passed between the model and the runtime.</p>
<p>For example, a tool declaration might be rendered as:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>&#x3C;|turn>system</span></span>
<span class="line"><span>You are a helpful assistant.</span></span>
<span class="line"><span></span></span>
<span class="line"><span>&#x3C;|tool></span></span>
<span class="line"><span>declaration:get_current_temperature{</span></span>
<span class="line"><span>  description:&#x3C;|"|>Gets the current temperature for a given location.&#x3C;|"|>,</span></span>
<span class="line"><span>  parameters:{</span></span>
<span class="line"><span>    properties:{</span></span>
<span class="line"><span>      location:{</span></span>
<span class="line"><span>        description:&#x3C;|"|>The city name, e.g. London&#x3C;|"|>,</span></span>
<span class="line"><span>        type:&#x3C;|"|>STRING&#x3C;|"|></span></span>
<span class="line"><span>      }</span></span>
<span class="line"><span>    },</span></span>
<span class="line"><span>    required:[&#x3C;|"|>location&#x3C;|"|>],</span></span>
<span class="line"><span>    type:&#x3C;|"|>OBJECT&#x3C;|"|></span></span>
<span class="line"><span>  }</span></span>
<span class="line"><span>}</span></span>
<span class="line"><span>&#x3C;tool|></span></span>
<span class="line"><span>&#x3C;turn|></span></span></code></pre>
<p>A model tool call might then appear as:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>&#x3C;|turn>model</span></span>
<span class="line"><span>&#x3C;|tool_call></span></span>
<span class="line"><span>get_current_temperature{</span></span>
<span class="line"><span>  location:&#x3C;|"|>London&#x3C;|"|></span></span>
<span class="line"><span>}</span></span>
<span class="line"><span>&#x3C;tool_call|></span></span>
<span class="line"><span>&#x3C;turn|></span></span></code></pre>
<p>And the runtime may return the result like this:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>&#x3C;|turn>user</span></span>
<span class="line"><span>&#x3C;|tool_response></span></span>
<span class="line"><span>get_current_temperature{</span></span>
<span class="line"><span>  temperature:&#x3C;|"|>18°C&#x3C;|"|>,</span></span>
<span class="line"><span>  condition:&#x3C;|"|>Cloudy&#x3C;|"|></span></span>
<span class="line"><span>}</span></span>
<span class="line"><span>&#x3C;tool_response|></span></span>
<span class="line"><span>&#x3C;turn|></span></span>
<span class="line"><span></span></span>
<span class="line"><span>&#x3C;|turn>model</span></span>
<span class="line"><span>The current temperature in London is 18°C and cloudy.</span></span>
<span class="line"><span>&#x3C;turn|></span></span></code></pre>
<p>The exact tokens may vary by model and serving stack, but the underlying point is the same: the template is now carrying state. It is not only formatting prose; it is preserving boundaries between tool definitions, tool invocations, tool results, user messages, assistant messages, hidden reasoning, and assistant continuation points. A misplaced delimiter can mean the difference between a functioning agent and one that refuses to call a tool, leaks internal scaffolding, or loses track of the previous step.</p>
<h3 id="the-failures-were-small-but-the-consequences-were-not">The Failures Were Small, but The Consequences Were Not</h3>
<p>The fixes and community patches around Gemma 4 clustered around a few categories. One class involved system-instruction and tool-call handling. That sounds mundane until you remember what a system instruction often contains in an agentic setting: when to use tools, how to handle destructive actions, how to report execution results, whether to ask for confirmation, and how to avoid pretending that a tool ran when it did not. If the tool declarations are rendered outside the expected control region, or if the assistant continuation is placed incorrectly, the model may not reliably see the operating contract it is supposed to follow.</p>
<p>A fragile rendering might separate the tool declaration from the system instruction in a way the model did not learn well:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>&#x3C;|turn>system</span></span>
<span class="line"><span>You are a helpful assistant.</span></span>
<span class="line"><span>&#x3C;turn|></span></span>
<span class="line"><span></span></span>
<span class="line"><span>&#x3C;|tool></span></span>
<span class="line"><span>declaration:bash{...}</span></span>
<span class="line"><span>&#x3C;tool|></span></span></code></pre>
<p>A more appropriate rendering may need to keep the tool declaration in the system-managed portion of the prompt, so the model receives the instruction and the available tools as one coherent operating context:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>&#x3C;|turn>system</span></span>
<span class="line"><span>You are a helpful assistant. Use tools when needed, but do not claim that a command has run until the tool response is returned.</span></span>
<span class="line"><span></span></span>
<span class="line"><span>&#x3C;|tool></span></span>
<span class="line"><span>declaration:bash{</span></span>
<span class="line"><span>  description:&#x3C;|"|>Execute a shell command.&#x3C;|"|>,</span></span>
<span class="line"><span>  parameters:{</span></span>
<span class="line"><span>    properties:{</span></span>
<span class="line"><span>      command:{</span></span>
<span class="line"><span>        description:&#x3C;|"|>The shell command to execute.&#x3C;|"|>,</span></span>
<span class="line"><span>        type:&#x3C;|"|>STRING&#x3C;|"|></span></span>
<span class="line"><span>      }</span></span>
<span class="line"><span>    },</span></span>
<span class="line"><span>    required:[&#x3C;|"|>command&#x3C;|"|>],</span></span>
<span class="line"><span>    type:&#x3C;|"|>OBJECT&#x3C;|"|></span></span>
<span class="line"><span>  }</span></span>
<span class="line"><span>}</span></span>
<span class="line"><span>&#x3C;tool|></span></span>
<span class="line"><span>&#x3C;turn|></span></span></code></pre>
<p>This kind of difference can look small in a pull request, but it is large in behavior. To the model, the template is part of the language of interaction. If the language changes, the model may still produce fluent text, but it may no longer produce the machine-readable action the runtime expects.</p>
<p>Another class of failures involved reasoning-channel leakage. Gemma 4-style agentic workflows can distinguish between internal reasoning and external output. Conceptually, a model might produce something like this:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>&#x3C;|channel>thought</span></span>
<span class="line"><span>The user is asking for a live directory listing, so I should use the shell tool rather than answer from memory.</span></span>
<span class="line"><span>&#x3C;channel|></span></span>
<span class="line"><span></span></span>
<span class="line"><span>&#x3C;|tool_call></span></span>
<span class="line"><span>bash{</span></span>
<span class="line"><span>  command:&#x3C;|"|>ls /tmp&#x3C;|"|></span></span>
<span class="line"><span>}</span></span>
<span class="line"><span>&#x3C;tool_call|></span></span></code></pre>
<p>In a well-integrated stack, the runtime would separate the reasoning from the tool call and expose only the appropriate fields to the application:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="json"><code><span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">{</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "reasoning_content"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "The user is asking for a live directory listing, so I should use the shell tool rather than answer from memory."</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "tool_calls"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">      "name"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "bash"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">      "arguments"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">        "command"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "ls /tmp"</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">      }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">  ]</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "content"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> null</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">}</span></span></code></pre>
<p>But in some setups, reasoning markers or reasoning text appeared in the visible assistant output. Sometimes special tokens were stripped before the parser saw them; sometimes the template replayed reasoning fields in a runtime that did not know how to hide them. The result could be output like:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>thought</span></span>
<span class="line"><span>The user is asking for a live directory listing, so I should use the shell tool rather than answer from memory.</span></span>
<span class="line"><span></span></span>
<span class="line"><span>I will now list the files.</span></span></code></pre>
<p>For a chatbot, this is embarrassing. For an agent, it is worse, because downstream parsers may interpret the leaked text as normal assistant content, the user interface may display internal scaffolding, and the system may lose the distinction between deliberation, action, and final answer. Some community patches handled this pragmatically by suppressing reasoning replay in environments that did not support reasoning channels correctly:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="jinja"><code><span class="line"><span style="color:#0550AE;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">{#</span><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"> Avoid replaying reasoning fields in runtimes that leak them </span><span style="color:#0550AE;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">#}</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#E6E0D4">{%</span><span style="color:#CF222E;--shiki-dark:#C792B8"> if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> message.role </span><span style="color:#CF222E;--shiki-dark:#D58A6A">==</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "assistant"</span><span style="color:#0550AE;--shiki-dark:#E6E0D4"> %}</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#E6E0D4">  {%</span><span style="color:#CF222E;--shiki-dark:#C792B8"> if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> message.content </span><span style="color:#0550AE;--shiki-dark:#E6E0D4">%}</span></span>
<span class="line"><span style="color:#953800;--shiki-dark:#E6E0D4">    {{ </span><span style="color:#1F2328;--shiki-dark:#E6E0D4">message</span><span style="color:#953800;--shiki-dark:#E6E0D4">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">content</span><span style="color:#953800;--shiki-dark:#E6E0D4"> }}</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#E6E0D4">  {%</span><span style="color:#CF222E;--shiki-dark:#C792B8"> endif</span><span style="color:#0550AE;--shiki-dark:#E6E0D4"> %}</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#E6E0D4">  {%</span><span style="color:#CF222E;--shiki-dark:#C792B8"> if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> message.tool_calls </span><span style="color:#0550AE;--shiki-dark:#E6E0D4">%}</span></span>
<span class="line"><span style="color:#953800;--shiki-dark:#E6E0D4">    {{ </span><span style="color:#1F2328;--shiki-dark:#E6E0D4">render_tool_calls</span><span style="color:#953800;--shiki-dark:#E6E0D4">(</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">message</span><span style="color:#953800;--shiki-dark:#E6E0D4">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">tool_calls</span><span style="color:#953800;--shiki-dark:#E6E0D4">) }}</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#E6E0D4">  {%</span><span style="color:#CF222E;--shiki-dark:#C792B8"> endif</span><span style="color:#0550AE;--shiki-dark:#E6E0D4"> %}</span></span>
<span class="line"><span style="color:#0550AE;--shiki-dark:#E6E0D4">{%</span><span style="color:#CF222E;--shiki-dark:#C792B8"> endif</span><span style="color:#0550AE;--shiki-dark:#E6E0D4"> %}</span></span></code></pre>
<p>That kind of patch is sensible, but it also shows the deeper ambiguity: who owns reasoning separation? Is it the chat template, the tokenizer, the inference server, the OpenAI-compatible shim, or the agent framework? In practice, it is a cross-layer contract, and when the layers disagree, the user sees a broken agent rather than a distributed systems problem.</p>
<p>A third class involved multimodal placeholders inside tool responses. This is easy to underestimate until you imagine an agent that calls a screenshot tool. The tool might return structured content:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="json"><code><span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">{</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "role"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "tool"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "name"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "take_screenshot"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "content"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">      "type"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "text"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">      "text"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "Screenshot captured."</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    }</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">      "type"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "image"</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">  ]</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">}</span></span></code></pre>
<p>A template that only extracts text could render the response as:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>&#x3C;|tool_response></span></span>
<span class="line"><span>take_screenshot{</span></span>
<span class="line"><span>  result:&#x3C;|"|>Screenshot captured.&#x3C;|"|></span></span>
<span class="line"><span>}</span></span>
<span class="line"><span>&#x3C;tool_response|></span></span></code></pre>
<p>The image has not merely been ignored; it has been disconnected from the prompt. The multimodal processor may still have an image tensor ready, but the prompt contains no placeholder telling the model where that image belongs. The corrected rendering needs to preserve the placeholder:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="text"><code><span class="line"><span>&#x3C;|tool_response></span></span>
<span class="line"><span>take_screenshot{</span></span>
<span class="line"><span>  result:&#x3C;|"|>Screenshot captured.&#x3C;|"|></span></span>
<span class="line"><span>}</span></span>
<span class="line"><span>&#x3C;|image|></span></span>
<span class="line"><span>&#x3C;tool_response|></span></span></code></pre>
<p>This is the kind of issue that makes clear why prompt templates are now infrastructure. In a text-only chatbot, dropping a non-text part might degrade an answer. In a multimodal agent, it can break alignment between the serialized prompt and the model inputs.</p>
<p>Streaming created its own class of problems. A non-streaming tool call is often easy to parse because the runtime receives the whole object at once:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="json"><code><span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">{</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "finish_reason"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "tool_calls"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "message"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">    "tool_calls"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">      {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">        "function"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">          "name"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "bash"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">          "arguments"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "{</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\"</span><span style="color:#0A3069;--shiki-dark:#A9B98C">command</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\"</span><span style="color:#0A3069;--shiki-dark:#A9B98C">:</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\"</span><span style="color:#0A3069;--shiki-dark:#A9B98C">ls /tmp</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\"</span><span style="color:#0A3069;--shiki-dark:#A9B98C">}"</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">      }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    ]</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">  }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">}</span></span></code></pre>
<p>In streaming mode, however, the same call may arrive in pieces:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="json"><code><span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">{</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "choices"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">      "delta"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">        "tool_calls"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">          {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">            "index"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">            "function"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">              "name"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "bash"</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">          }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        ]</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">      }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">  ]</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">}</span></span></code></pre>
<p>Then:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="json"><code><span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">{</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "choices"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">      "delta"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">        "tool_calls"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">          {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">            "index"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">            "function"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">              "arguments"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "{</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\"</span><span style="color:#0A3069;--shiki-dark:#A9B98C">command</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\"</span><span style="color:#0A3069;--shiki-dark:#A9B98C">:</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\"</span><span style="color:#0A3069;--shiki-dark:#A9B98C">ls"</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">          }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        ]</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">      }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">  ]</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">}</span></span></code></pre>
<p>Then:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="json"><code><span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">{</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">  "choices"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">      "delta"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">        "tool_calls"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">          {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">            "index"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">            "function"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> {</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">              "arguments"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> " /tmp</span><span style="color:#CF222E;--shiki-dark:#D8B56D">\"</span><span style="color:#0A3069;--shiki-dark:#A9B98C">}"</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">          }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">        ]</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">      }</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#116329;--shiki-dark:#A9B98C">      "finish_reason"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> "tool_calls"</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    }</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">  ]</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">}</span></span></code></pre>
<p>If the client or agent framework does not reconstruct those fragments correctly, the model may appear to have failed to use tools even though the tool call was produced. The bug is then not in the model’s capability, but in the protocol bridge between model output and agent action.</p>
<h3 id="the-old-template-was-prose-the-new-template-is-an-interface">The Old Template was Prose; The New Template is an Interface</h3>
<p>The easiest way to frame this is to borrow a concept from systems programming. An ABI defines how compiled components call each other: which values go where, how arguments are passed, how return values are represented, and which conventions both sides must obey. If the ABI is wrong, two correct components can fail together.</p>
<p>Agentic LLMs now have a similar boundary. The model has learned a control language. The serving runtime has parsers. The tool framework has schemas. The client may expect OpenAI-style tool calls. The UI may expect visible content and hidden reasoning to be separated. The chat template sits in the middle, translating conversation state into the model’s expected language and back again.</p>
<p>A typical agent loop depends on this sequence being exact: the runtime renders the system instruction, renders available tools, appends the user request, waits for either a final answer or a tool call, parses the tool call, executes the tool, renders the tool result, and prompts the model to continue. If the tool declaration is malformed, the model may not know what it can call. If the reasoning boundary is mishandled, hidden text can leak. If the tool response is rendered incorrectly, the model may not understand the result. If the assistant continuation marker is wrong, the model may continue the wrong turn.</p>
<p>This is why early adopters patched templates themselves. They were not arguing about punctuation. They were repairing the interface between the model and the rest of the system.</p>
<h3 id="so-what">So What?</h3>
<p>The practical lesson is straightforward: if you are building agents, treat the chat template as part of the model artifact. Do not treat it as sample code, documentation, or a convenience snippet copied from a model card. It is part of the runtime contract, and it should be pinned, tested, reviewed, and versioned along with the weights, tokenizer, serving engine, tool parser, reasoning parser, client SDK, and agent framework.</p>
<p>A serious agent deployment should know exactly which versions of these components belong together:</p>
<ul>
<li>model weights;</li>
<li>tokenizer;</li>
<li>chat template;</li>
<li>serving runtime;</li>
<li>tool parser;</li>
<li>reasoning parser;</li>
<li>structured-output backend;</li>
<li>client SDK;</li>
<li>agent framework; and</li>
<li>frontend or IDE integration.</li>
</ul>
<p>Changing any one of them can break the others. A new runtime may strip special tokens earlier than before. A new template may render tool responses differently. A client SDK may alter streaming reconstruction. A model conversion may preserve weights but lose the original template. A quantized local package may work for chat but fail in multi-turn tool use. These are not theoretical worries; they are the ordinary failure modes of agentic systems.</p>
<p>The test plan should change accordingly. It is not enough to ask whether the model gives a good answer to a prompt. Teams should test whether the full loop works:</p>
<ul>
<li>Single-turn chat renders correctly.</li>
<li>Multi-turn chat preserves roles and turn boundaries.</li>
<li>System instructions remain in the expected control region.</li>
<li>Tool declarations render with valid schemas.</li>
<li>The model emits a valid tool call when a tool is needed.</li>
<li>The runtime parses that tool call correctly.</li>
<li>Streaming tool calls are reconstructed correctly.</li>
<li>Tool responses are rendered back into the conversation correctly.</li>
<li>The model resumes after a tool response instead of starting over.</li>
<li>Reasoning content is separated from user-visible content.</li>
<li>Thinking-off mode does not break structured output.</li>
<li>Multimodal user input preserves image/audio placeholders.</li>
<li>Multimodal tool responses preserve image/audio placeholders.</li>
<li>JSON schema constraints are actually enforced.</li>
<li>A five-turn tool-using workflow survives without format drift.</li>
</ul>
<p>The important question is not merely, “Can the model answer?” It is, “Can the system act, observe, continue, and stay inside the contract?”</p>
<h3 id="why-this-episode-matters-beyond-gemma-4">Why This Episode Matters Beyond Gemma 4</h3>
<p>The Gemma 4 template issue is interesting because it captures a broader transition in LLM engineering. The industry is moving from chat to action. In chat, the human is the parser of last resort; if the model says something slightly odd, the user can infer what it meant. In agentic workflows, the next consumer is often code. Code is less forgiving. It needs typed calls, reliable delimiters, valid schemas, and predictable stop conditions.</p>
<p>That difference raises the engineering standard. A model may have strong reasoning ability, long context, good multimodal understanding, and impressive benchmark results, yet still feel unreliable if the protocol around it is loose. The reverse is also true: a smaller model with a clean, well-tested tool interface can feel more useful in production than a stronger model wrapped in a brittle integration layer.</p>
<p>This is the quiet lesson behind the prompt-template fiasco. We often talk about prompts as if they are natural language instructions, and sometimes they are. But in modern agent systems, the most important parts of the prompt are often not natural language at all. They are delimiters, schemas, placeholders, channel markers, continuation tokens, and parser assumptions. They are the parts humans barely notice and machines cannot forgive.</p>
<p>The final takeaway is not “be careful with Gemma 4,” nor is it “large model releases are hard.” The takeaway is more general and more useful: as LLM applications become agents, the interface between model and runtime becomes a first-class engineering surface. The prompt template is one of those interfaces. Treat it with the same seriousness you would give to an API contract, because in an agentic system, that is what it has become.</p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="ai" /><category term="agent" /><category term="agentic ai" /><category term="gemma" /><category term="google" /><summary type="html">The Gemma 4 prompt-template dust-up was not really about Jinja syntax or special tokens. It was about a deeper shift in AI systems - as LLMs move from chatbots to agents, the prompt template becomes part of the runtime contract. When that contract is even slightly wrong, tools fail, reasoning leaks, multimodal inputs fall out of alignment, and a capable model can look broken.</summary></entry><entry><title type="html">What Even is an Agent?</title><link href="https://victorangeloblancada.github.io/blog/2026/06/30/what-even-is-an-agent.html" rel="alternate" type="text/html" title="What Even is an Agent?" /><published>2026-06-30T00:00:00.000Z</published><updated>2026-06-30T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2026/06/30/what-even-is-an-agent</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2026/06/30/what-even-is-an-agent.html"><![CDATA[<p>Microsoft Copilot uses the word “agent” to name assistants inside Microsoft 365: Researcher, Word Agent, Excel Agent, PowerPoint Agent, and others. The usage is reasonable. A user deciding how to draft a document, analyze a workbook, build a deck, or produce a research brief needs a clear product label, not an architecture diagram. Microsoft’s term works well at that layer: it tells the user which kind of help to invoke.</p>
<p>Engineers, however, need a stricter definition. In current frontier practice, an agent is not merely a named assistant. An agent is an language model-driven execution loop: a system that can reason about a goal, choose tools, act on an environment, observe the result, update state, and continue until the work is done or stopped. OpenAI’s agent documentation describes agents around models, instructions, tools, guardrails, handoffs, MCP servers, and structured outputs; its broader framing treats agents as applications that plan, call tools, collaborate, and maintain state for multi-step work.</p>
<p>The difference is not a quarrel over words. It changes what engineers can build, evaluate, govern, and trust. A Copilot agent is usually a packaged productivity capability. An agentic workflow is an architecture for delegated work.</p>
<h2 id="the-original-definition-of-an-agent-was-useful-but-incomplete">The Original Definition of an Agent was Useful but Incomplete</h2>
<p>The original LLM-era definition of an agent was simple: an agent is a language model prompted with a persona that modifies its behavior.</p>
<p>Under that definition, an agent was a model prompted to behave as something: a researcher, analyst, planner, tutor, engineer, or critic. The system prompt gave the model its role, tone, goals, and boundaries. A “data engineering agent” would respond like a data engineer. A “research agent” would reply like a researcher.</p>
<p>In hindsight, that definition was useful but would turn out to be incomplete.</p>
<p>A persona changes the model’s stance. It does not give the system hands. A model with a researcher persona can write in the style of a researcher.</p>
<p>A true agentic workflow, however, can search, retrieve, compare, cite, draft, revise, call tools, inspect files, execute code, and decide whether another step is needed.</p>
<p>The old definition described behavior at the language layer, but the modern definition describes behavior at the system layer.</p>
<h2 id="a-modern-agent-is-a-loop">A Modern Agent is a Loop</h2>
<p>A more precise working definition for an agent would be:</p>
<blockquote>
<p>An agent is an LLM-centered control system that uses instructions, tools, state, and an execution loop to make progress toward a goal under constraints.</p>
</blockquote>
<p>The decisive word here in this definition of an agent is <strong>loop</strong>.</p>
<p>Modern agentic workflows follow the following rhythm:</p>
<ol>
<li>The user supplies a goal</li>
<li>The model interprets the goal</li>
<li>The system exposes tools and context</li>
<li>The model chooses an action</li>
<li>The environment feedbacks an observation</li>
<li>The model updates its plan</li>
<li>The loop continues until the system reaches a stopping condition such as meeting the goal</li>
</ol>
<p>The illustrious Claude Code is a useful, well-known, concrete example. Anthropic describes Claude Code as an agent that reads a codebase, edits files, and runs commands across a terminal, IDE, desktop app, and browser.  Anthropic also describes it as an agentic coding system that searches codebases, edits across files, executes through the toolchain, runs tests, and handles CI failures.</p>
<p>Claude Code as an example marks a useful boundary: a chatbot can discuss a repository, but an agentic coding system can work inside one.</p>
<p>The model supplies judgment, but the harness supplies agency.</p>
<h2 id="microsofts-usage-is-not-wrong-but-it-is-product-oriented">Microsoft’s Usage is Not Wrong, but it is Product-Oriented</h2>
<p>Microsoft’s use of “agent” is best viewed as a product abstraction for marketing purposes.</p>
<p>Researcher in Microsoft 365 Copilot is described as an intelligent assistant for complex, multi-step research that can draw from work content and the web and produce source-cited reports.  Microsoft also describes Word, Excel, and PowerPoint Agents as chat-first co-creators that help create documents, spreadsheets, and presentations inside Microsoft 365 Copilot.</p>
<p>This naming convention makes sense for non-technical users. Someone choosing between standard Copilot chat and Researcher does not need a lecture on orchestration. The product label instead says, in effect, “Use this assistant for this kind of work.”</p>
<p>A problem, however, appears when engineers carry that product meaning into architecture discussions.</p>
<p>A Copilot agent may potentially contain agentic techniques internally. It may possibly retrieve context, reason over sources, use internal tools, ask clarifying questions, generate artifacts, and operate within Microsoft 365’s security and compliance boundaries. Researcher, for example, is positioned as deeper and more multi-step than standard Copilot chat.</p>
<p>The user-facing name, however, does not specify the machinery: it does not tell the engineer what tools are exposed, which actions are possible, how state is represented, whether the loop is inspectable, what approvals are enforced, how failures are retried, or whether the workflow can be extended with custom skills, MCP servers, sub-agents, or sandboxes.</p>
<p>Microsoft’s term is useful. It is also not exactly wrong, but it is simply broader and looser than the technical term.</p>
<h2 id="a-better-metaphor-for-agents-are-workcells">A Better Metaphor for Agents are Workcells</h2>
<p>If a persona is a costume and a Copilot agent is a specialist desk, then an agentic workflow is a workcell.</p>
<p>In lean manufacturing, a workcell has tools, inputs, procedures, safety rules, inspection points, escalation paths, and output criteria. Its behavior comes not from the name on the station but from the system built around it.</p>
<p>The same logic from lean manufacturing also applies to agents. A serious agentic workflow needs:</p>
<ul>
<li>a model selected for the task</li>
<li>instructions that define role, constraints, and output contract</li>
<li>tools that let the system read, write, compute, search, execute, or delegate</li>
<li>state that records progress, intermediate artifacts, and prior observations</li>
<li>permissions that decide what the model may do</li>
<li>guardrails that block unsafe or low-confidence actions</li>
<li>checkpoints and rollback for recoverability</li>
<li>traces and logs for inspection</li>
<li>evaluations that judge completed work, not fluent prose</li>
</ul>
<p>OpenAI’s tooling makes these concerns explicit through tools, guardrails, handoffs, sessions, tracing, sandbox agents, and human-in-the-loop mechanisms.  Anthropic’s Claude Code work similarly emphasizes IDE integration, checkpointing, subagents, hooks, and SDK-level customization for more autonomous operation.</p>
<p>An agent is therefore less like a clever yet isolated employee and more like a governed machine with a model in the control center.</p>
<h2 id="mcp-makes-the-boundary-visible">MCP Makes the Boundary Visible</h2>
<p>The Model Context Protocol helps explain why modern agentic systems feel different from application assistants.</p>
<p>Anthropic introduced MCP as an open standard for connecting AI assistants to systems where data lives, including repositories, business tools, and development environments.  The MCP specification describes a standard way for LLM applications to integrate with external data sources and tools, with servers exposing resources, prompts, and tools to clients.</p>
<p>MCP turns an agent from a prompt into a participant in an ecosystem.</p>
<p>Before MCP and MCP-like patterns, each tool connection tended to be bespoke. After MCP, an agentic system can attach to standardized capabilities: repositories, databases, ticketing systems, filesystems, browsers, internal APIs, business tools, etc.</p>
<p>MCP is not the agent, but MCP is its plumbing. The agent is the controller that decides which pipe to use, when to use it, and what to do with the result.</p>
<p>The shift to MCP changes the engineering questions. The interesting questions are no longer limited to what the assistant should say, what tone it should use, or what content it should generate. The more important questions are now:</p>
<ul>
<li>What can the system read?</li>
<li>What can the system change?</li>
<li>Which tools are available at each step?</li>
<li>What state persists across steps?</li>
<li>What actions require approval?</li>
<li>How are tool calls logged?</li>
<li>How does the loop recover from error?</li>
<li>What proves that the task is complete?</li>
</ul>
<p>Those are questions that define agentic work.</p>
<h2 id="why-does-this-distinction-even-matter">Why Does This Distinction Even Matter?</h2>
<p>Teams that live in enterprise platforms such as Microsoft 365, Teams, SharePoint, Power Platform, Fabric, Azure, Purview, and Graph can easily conclude that they already understand agents because Copilot exposes an Agents menu. That conclusion is partly true but also partly limiting.</p>
<p>Copilot agents are useful for work that naturally ends in a Microsoft 365 artifact: a report, deck, spreadsheet, meeting summary, email, or structured answer. They are purpose-built for the productivity surface.</p>
<p>Agents, in the context of agentic workflows, address a different class of problem: tasks where the system must operate across tools, inspect intermediate results, repair mistakes, and continue. Examples include:</p>
<ul>
<li>refactoring a codebase</li>
<li>migrating a data pipeline</li>
<li>debugging CI failures</li>
<li>reconciling semantic model changes</li>
<li>generating and testing SQL transformations</li>
<li>validating data quality rules</li>
<li>repairing orchestration jobs</li>
<li>creating pull requests</li>
<li>querying multiple operational systems</li>
<li>coordinating several specialized workers</li>
</ul>
<p>A Copilot agent may help with pieces of these tasks, but an actual agentic workflow is designed to own the loop.</p>
<p>For engineers, the risk is not that the terminology is wrong. The risk is category error: agents as a product label and a runtime architecture answer different questions.</p>
<p>The product question is: <strong>Which assistant should the user invoke?</strong><br>
The engineering question is: <strong>What system can safely perform this work?</strong></p>
<h2 id="a-practical-test">A Practical Test</h2>
<p>A simple test separates the two meanings:</p>
<blockquote>
<p>If the system mainly gives a named assistant for a bounded user task, “agent” is being used in the product sense.<br>
If the system can choose tools, act on an environment, observe results, update state, and iterate toward a goal, “agent” is being used in the technical sense.</p>
</blockquote>
<p>This test is mechanical by design: it avoids arguments about branding and focuses on capability.</p>
<p>A model with a persona is not enough, a chat interface is not enough, a menu item called “Agent” is not enough - a truly agentic workflow only exists when the model participates in a governed action loop.</p>
<h2 id="a-glossary-of-cleaner-vocabulary">A Glossary of Cleaner Vocabulary</h2>
<p>The precise vocabulary below prevents confusion without forcing anyone to abandon useful product terms.</p>
<table>
<thead>
<tr>
<th>Term</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td>Persona</td>
<td>The role, tone, and behavioral frame given to a model through instructions.</td>
</tr>
<tr>
<td>Assistant</td>
<td>A conversational system that helps a user produce an answer or artifact.</td>
</tr>
<tr>
<td>Copilot agent</td>
<td>A Microsoft-branded, task-oriented Copilot capability inside a product surface.</td>
</tr>
<tr>
<td>Tool</td>
<td>A callable capability that lets the system read, write, compute, retrieve, execute, or delegate.</td>
</tr>
<tr>
<td>MCP server</td>
<td>A standardized provider of context, prompts, or tools to an AI application.</td>
</tr>
<tr>
<td>Harness</td>
<td>The runtime code that manages tools, state, permissions, retries, approvals, and stopping rules.</td>
</tr>
<tr>
<td>Sub-agent</td>
<td>A specialized agent or worker invoked by another agent or orchestrator.</td>
</tr>
<tr>
<td>Agentic workflow</td>
<td>The full loop in which an LLM uses tools and state to act iteratively toward a goal.</td>
</tr>
</tbody>
</table>
<p>This glossary keeps both meanings intact.</p>
<p>Copilot agents are agents in the Microsoft product sense. Agents in agentic workflows are agents in the engineering-system sense. The second meaning is the one engineers should use when designing systems.</p>
<h2 id="conclusion">Conclusion</h2>
<p>The word “agent” began as a light abstraction: a language model with a persona. Enterprise software such as Microsoft then made the word accessible by applying it to named Copilot experiences that help users perform specific kinds of work inside Microsoft 365. That product usage is understandable and often helpful.</p>
<p>The technical frontier has made the word stricter.</p>
<p>A modern agent is not defined by its name, persona, or chat surface. A modern agent is defined by a loop: model, tools, state, action, observation, control, and termination.</p>
<p>Copilot agents package assistance, while agentic workflows execute governed work.</p>
<p>Both these definitions matter and they should not be conflated.</p>
<p>The cleanest formulation today is:</p>
<blockquote>
<p>A persona tells a model how to behave.<br>
A Copilot agent tells a user which assistant to use.<br>
An agentic workflow gives a model the machinery to act.</p>
</blockquote>
<p>This is the bridge between the two worlds of product and engineering.</p>
<h2 id="summary-comparison-table">Summary Comparison Table</h2>
<table>
<thead>
<tr>
<th>Dimension</th>
<th>Microsoft Copilot “Agent”</th>
<th>Agentic Workflow</th>
</tr>
</thead>
<tbody>
<tr>
<td>Primary meaning</td>
<td>A named Microsoft 365 Copilot experience for a bounded class of work.</td>
<td>A system in which a model acts through an iterative loop.</td>
</tr>
<tr>
<td>Best mental model</td>
<td>A specialist inside a productivity suite.</td>
<td>A controlled execution loop.</td>
</tr>
<tr>
<td>User experience</td>
<td>Copilot Chat, Word, Excel, PowerPoint, Researcher, or another Microsoft 365 surface.</td>
<td>Terminal, IDE, sandbox, MCP client, workflow engine, browser, CI system, or custom application.</td>
</tr>
<tr>
<td>Core abstraction</td>
<td>Product capability.</td>
<td>Runtime architecture.</td>
</tr>
<tr>
<td>Tool use</td>
<td>Often present but hidden behind the product boundary.</td>
<td>Explicit, inspectable, extensible, and governed.</td>
</tr>
<tr>
<td>Autonomy</td>
<td>Scoped to the Microsoft 365 task and application model.</td>
<td>Scoped by tools, permissions, state, approvals, sandboxing, and stopping rules.</td>
</tr>
<tr>
<td>Extensibility</td>
<td>Mostly through Microsoft-provided surfaces and enterprise integrations.</td>
<td>Through harnesses, MCP servers, skills, sub-agents, code sandboxes, structured outputs, guardrails, and orchestration logic.</td>
</tr>
<tr>
<td>Correct technical sense of “agent”</td>
<td>A useful product term, but not the strict engineering definition.</td>
<td>The strict engineering definition: an LLM-driven system that can act, observe, and iterate toward a goal.</td>
</tr>
<tr>
<td>Practical test</td>
<td>“Which Copilot capability should I use?”</td>
<td>“What loop, tools, state, and controls let the model complete the task?”</td>
</tr>
</tbody>
</table>]]></content><author><name>Victor Angelo Blancada</name></author><category term="ai" /><category term="agent" /><category term="agentic ai" /><category term="claude" /><category term="copilot" /><category term="featured" /><summary type="html">Everyone talks about AI agents, but very few people mean the same thing when they do.</summary></entry><entry><title type="html">Dissecting a Power BI Dashboard</title><link href="https://victorangeloblancada.github.io/blog/2024/12/13/dissect-pbix.html" rel="alternate" type="text/html" title="Dissecting a Power BI Dashboard" /><published>2024-12-13T00:00:00.000Z</published><updated>2024-12-13T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2024/12/13/dissect-pbix</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2024/12/13/dissect-pbix.html"><![CDATA[<p>In this article, we will thoroughly explore the structure and contents of a Power BI dashboard (PBIX) file. Like most Microsoft Office file formats, a PBIX file is actually a zipped folder containing various directories and files which work together to form the dashboard we see. Understanding the underlying architecture of a Power BI report is crucial for both users and developers, as it enables a deeper appreciation of how data is structured, processed, and displayed.</p>
<p>This guide will break down the various directories and files found inside the zipped folder of a PBIX file, explain their purpose, and show how these elements work together to create the final dashboard. We will also delve into the core <code>Layout</code> file, which plays a key role in defining how data is translated into charts, tables, and other visualizations within the dashboard.</p>
<h2 id="overview-of-a-power-bi-report-package-structure">Overview of a Power BI Report Package Structure</h2>
<p>A PBIX file is typically organized in a package with a clearly defined directory structure. This structure can be exposed by changing the file extension from PBIX to ZIP, then unzipping the resulting zipped folder to reveal its contents.</p>
<p>Each folder and file within this structure serves a specific purpose in making the dashboard work the way it does. Below is an overview of the key directories and files in the report package:</p>
<p><em>rels<br>
|</em> .rels<br>
docProps<br>
|_ custom.xml<br>
Report<br>
|_ Layout<br>
|_ StaticResources\SharedResources\BaseThemes<br>
|_ CY24SU08.json<br>
[Content_Types].xml<br>
DataModel<br>
DiagramLayout<br>
Metadata<br>
SecurityBindings<br>
Settings<br>
Version</p>
<p>Each of these components plays a vital role in ensuring that the report is displayed and interacts as intended. We will break down each section to understand what it contains and how it contributes to the functionality of the dashboard.</p>
<h3 id="the_relsdirectory">The <code>_rels</code> Directory</h3>
<p>The <code>_rels</code> directory is one of the most critical directories in the Power BI report package. It contains the <code>.rels</code> file, which manages the relationships between different parts of the report.</p>
<p>The <code>.rels</code> file stores the relationships between various elements of the report. For example, it could define how the layout file is connected to the static resources or how the report interacts with external data models. The <code>.rels</code> file ensures that when the report is opened, the application knows how to load and render the different parts of the report package correctly.</p>
<h3 id="thedocpropsdirectory">The <code>docProps</code> Directory</h3>
<p>The <code>docProps</code> directory contains metadata files that provide additional information about the report. Metadata is essential for defining the context and properties of the report, which can be helpful for tracking and reporting purposes.</p>
<p>The <code>custom.xml</code> file stores user-defined properties, such as the author of the report, version number, or other custom attributes relevant to the report. For example, it could store details about the report’s creation date, the business unit it pertains to, or custom fields that help categorize and filter the report.</p>
<h3 id="thereportdirectory">The <code>Report</code> Directory</h3>
<p>The <code>Report</code> directory is where the majority of the report’s configuration and layout components are found. It contains the layout file, static resources, and any base themes that are used to define the visual look and feel of the report. These elements are key in defining how the data is visually presented.</p>
<h4 id="layout">Layout</h4>
<p>The layout file is the core of the report. It defines the visual structure of the report, specifying how the data will be displayed in charts, tables, and other visual elements. The layout file determines the positions of various components on the report canvas and ensures that the visual elements are placed and sized correctly.</p>
<p>As an example, the <code>Layout</code> file may define the report’s main visual container, which could hold a chart, such as a column chart. The chart may be configured to display data from a table named <code>Table1</code>, and the layout also specifies how the data is grouped by different time periods (such as year, quarter, month, and day). It also defines how specific measures, like the quantity of items sold, are aggregated and visualized in the chart.</p>
<p>The layout file also specifies the data projections (i.e., the data elements to be displayed in the chart), the filters that should be applied to restrict the data, and how different data roles (such as categories, series, and rows) are mapped to the visual components.</p>
<h4 id="staticresourcessharedresourcesbasethemes">StaticResources\SharedResources\BaseThemes</h4>
<p>The <code>CY24SU08.json</code> file defines the visual theme for the report. Themes are used to maintain consistency in design across different reports and dashboards. This JSON file stores information about color schemes, fonts, and other stylistic choices that apply throughout the report. By using a theme like <code>CY24SU08.json</code>, the report ensures that all visual elements are cohesive and adhere to a specific design aesthetic, whether for brand consistency or user experience.</p>
<h3 id="other-key-files-in-the-report-package">Other Key Files in the Report Package</h3>
<p>In addition to the layout and theme resources, there are several other important files that contribute to the functionality of the report.</p>
<h3 id="content_typesxml">[Content_Types].xml</h3>
<p>The <code>[Content_Types].xml</code> file is essential for defining the MIME types for the various parts of the report package. It helps the report-rendering system determine how to process different file types within the package, such as XML files, images, or JSON files. This file serves as a map for the content and ensures that each component is handled correctly.</p>
<h3 id="datamodel">DataModel</h3>
<p>The <code>DataModel</code> binary contains the underlying schema and data definitions that drive the report. It includes details such as tables, columns, and relationships between different datasets. This folder is vital because it defines the data structure that feeds into the visual components, allowing the dashboard to present relevant and accurate information. This file usually cannot be read because its encoding is not supported by most text editors.</p>
<h3 id="diagramlayout">DiagramLayout</h3>
<p>The <code>DiagramLayout</code> directory may contain files that define additional visual elements, such as diagrams, flowcharts, or other graphical representations that complement the report’s main data visuals. While the core report may focus on charts and tables, diagram layouts can help explain complex data relationships or workflows visually.</p>
<h3 id="metadata">Metadata</h3>
<p>Metadata files store information about the report itself, such as the author, version history, creation date, and other contextual information. These files are crucial for tracking changes and updates to the report, ensuring that the correct version is always used and providing information about the report’s lifecycle.</p>
<h3 id="securitybindings">SecurityBindings</h3>
<p>The <code>SecurityBindings</code> binary handles the security aspects of the report, ensuring that sensitive data is protected. It defines access control and permissions, specifying which users or groups can view certain sections or interact with specific data points within the report. This is particularly important for reports containing sensitive or confidential information. This file usually cannot be read because its encoding is not supported by most text editors.</p>
<h3 id="settings">Settings</h3>
<p>The <code>Settings</code> directory contains configuration files that manage preferences or environment-specific settings for the report. These settings ensure that the report behaves as expected across different platforms and environments, whether it’s running locally or being accessed via a web service.</p>
<h3 id="version">Version</h3>
<p>The <code>Version</code> file indicates the current version of the report or report package. This is important for version control, ensuring that users are working with the most up-to-date version of the report and helping to prevent the use of outdated or deprecated files.</p>
<h3 id="parsing-and-interpreting-the-layout-file">Parsing and Interpreting the Layout File</h3>
<p>Let’s take a closer look at the <code>Layout</code> file, which is at the heart of how the report’s visual elements are configured. The layout file is a configuration file, often in JSON format, that dictates the presentation and interaction of the data in the report.</p>
<h3 id="key-aspects-of-the-layout-file">Key Aspects of the Layout File</h3>
<ol>
<li><strong>Visual Containers</strong>: The layout file defines a visual container (e.g., a chart or table) that holds the report’s data. For example, in this case, the container holds a column chart, and the file specifies its position, size, and dimensions on the report canvas.</li>
<li><strong>Projections and Querying</strong>: The <code>projections</code> section in the layout file specifies which data elements should be displayed. This includes categories, series, and rows, as well as aggregation functions (e.g., sum, count). This section ensures that the right data is selected and formatted for visualization.</li>
<li><strong>Filters</strong>: Filters are used to limit which data points are shown in the report. These filters can be based on specific criteria (e.g., dates, categories, or regions) to ensure that the report displays only relevant information.</li>
<li><strong>Data Role Mapping</strong>: The <code>Layout</code> file maps the data roles to the visual components. For instance, categories might be mapped to the x-axis, while series could be used for color-coding the data. These mappings help translate raw data into meaningful visualizations.</li>
<li><strong>Execution Metrics</strong>: The layout file may also include execution metrics that track how efficiently the report is rendering, especially when dealing with large datasets. This ensures that the report performs well and loads quickly.</li>
</ol>
<h2 id="summary">Summary</h2>
<p>Understanding the inner workings of a Power BI report requires more than just familiarity with the visual components. The underlying structure, including the directories and files that make up the report package, plays a crucial role in ensuring that the report functions correctly. Each file, from the layout to metadata and security settings, contributes to the overall experience of the report, ensuring that the data is presented clearly, accurately, and securely.</p>
<p>The <code>Layout</code> file, in particular, is essential for defining how data is visualized. By carefully structuring the report’s visuals and ensuring that the data is aggregated and displayed correctly, the layout file is a key component in transforming raw data into actionable insights. When working with or developing Power BI reports, understanding these elements will help you navigate and optimize the report for both users and analysts alike.</p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="power bi" /><category term="dashboard" /><category term="microsoft" /><summary type="html">PBIX files are zipped folders and examining their contents can shed light on how Power BI translates data into visualizations.</summary></entry><entry><title type="html">The Negative Effect of Pruning Large Language Models on Finetuning</title><link href="https://victorangeloblancada.github.io/blog/2024/05/31/llm-pruning-finetuning.html" rel="alternate" type="text/html" title="The Negative Effect of Pruning Large Language Models on Finetuning" /><published>2024-05-31T00:00:00.000Z</published><updated>2024-05-31T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2024/05/31/llm-pruning-finetuning</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2024/05/31/llm-pruning-finetuning.html"><![CDATA[<p>Large language models (LLMs) such as Llama and Falcon often serve as foundational models, the bedrock upon which more specialized AI applications are built through finetuning. However, the computational demands of these models pose a significant challenge for both inference and finetuning. Techniques in optimizing neural networks, particularly pruning, may offer a solution by reducing the number of model parameters for more efficient inference without a consequent large negative effect on accuracy. However, while pruning reduces computational requirements for inference, it also reduces a model’s ability to be finetuned, as will be explained in this article. This poses a challenge for organizations wishing to leverage AI language models for specialized use.</p>
<p>Pruning aims to make neural networks more efficient by removing unnecessary connections within the model. Common pruning methods include:</p>
<ul>
<li>Magnitude-based pruning: Eliminates connections with weights below a certain threshold.</li>
<li>Structured pruning: Removes entire groups of neurons or connections.</li>
</ul>
<p>Pruning is effective in reducing the size of different models, but given the recent gold rush to deploy language models, it is important to look at its effect on these language models.</p>
<p>For large language models like GPT, Llama, and Falcon, pruning offers inference efficiency gains. However, pruning may potentially compromise adaptability during fine-tuning. Redundant neurons are often useful for encoding new information from finetuning, but they are often removed during the pruning process, thus hindering the model’s capacity to learn new information.</p>
<p>To illustrate, consider a language model pre-trained on a diverse corpus of text. The redundancies in the model allow it to store the same information across different layers and weights. During finetuning for a sentiment analysis, the model may learn to differentiate words or phrases with the same denotation (explicit meaning) but different connotation (culturally-specific nuance) based on the provided training data and can use the neural network’s redundancies to store these differences. However, if these redundancies had been pruned, the model loses the flexibility to encode these more nuanced relationships, leading to suboptimal performance.</p>
<p>Organizations therefore face a strategic decision regarding pruning:</p>
<ul>
<li>Resource Constraints: Pruned FLMs offer efficiency benefits, suitable for resource-constrained environments where computational resources are limited. Despite potential limitations in fine-tuning, efficiency gains may outweigh adaptability concerns.</li>
<li>Task Complexity: For intricate tasks demanding precise adaptation, unpruned language models with full learning capacity are desired. These models will excel in capturing complex relationships from new information, essential for nuanced tasks like legal document analysis.</li>
</ul>
<p>Pruning large language models presents a trade-off, optimizing efficiency at the expense of adaptability later on. Striking the right balance is paramount as organizations seek to leverage AI in an evolving business environment.</p>
<p>My advice would be to use the largest language model which an organization’s hardware permits as a foundational model for finetuning, then only start pruning the model after finetuning is finished.</p>
<p>By understanding the nuances of pruning and its implications for fine-tuning, businesses can make informed decisions, harnessing the power of language models while achieving desired outcomes in specialized domains.</p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="ai" /><category term="artificial intelligence" /><category term="llm" /><category term="large language model" /><category term="pruning" /><category term="fine-tuning" /><summary type="html">Pruning large language models make them easier to deploy but also make them harder to finetune for the specific tasks organizations may want them to perform.</summary></entry><entry><title type="html">Fuzzy Matching in Python</title><link href="https://victorangeloblancada.github.io/blog/2024/03/02/fuzzy-matching-in-python.html" rel="alternate" type="text/html" title="Fuzzy Matching in Python" /><published>2024-03-02T00:00:00.000Z</published><updated>2024-03-02T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2024/03/02/fuzzy-matching-in-python</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2024/03/02/fuzzy-matching-in-python.html"><![CDATA[<p><em>This is part of a series of short blog posts about automating repetitive work using Python.</em></p>
<p>This is how to perform partial matching or fuzzy matching in Python using <a href="https://github.com/seatgeek/thefuzz">TheFuzz library</a>. This is useful for scenarios such as matching addresses written in different formats (e.g., New York vs NewYork vs New York City) or fixing typos in categorical data (e.g., Brusels can be matched to Brussels).</p>
<p>Start by importing process from TheFuzz:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="python"><code><span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Import fuzzy matching libraries</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">from</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> thefuzz </span><span style="color:#CF222E;--shiki-dark:#C792B8">import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> process</span></span></code></pre>
<p>Assuming you have two Pandas dataframes, df_1 and df_2, which you want to join on col_1 and col_2, respectively, you can run the following code to show matches from df_2 in df_1:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="python"><code><span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Create column of match result object. The extractOne function only returns the best match.</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match Result'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">] </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> df_1[col_1]</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">apply(</span><span style="color:#CF222E;--shiki-dark:#C792B8">lambda</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> x</span><span style="color:#1F2328;--shiki-dark:#80B9D0">: process</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">extractOne(x</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> df_2[col_2]))</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Extract fuzzy matching string from result</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">] </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match Result'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">]</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">apply(</span><span style="color:#CF222E;--shiki-dark:#C792B8">lambda</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> x</span><span style="color:#1F2328;--shiki-dark:#80B9D0">: x[</span><span style="color:#0550AE;--shiki-dark:#D8B56D">0</span><span style="color:#1F2328;--shiki-dark:#80B9D0">])</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Extract fuzzy matching score</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match Score'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">] </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match Result'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">]</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">apply(</span><span style="color:#CF222E;--shiki-dark:#C792B8">lambda</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> x</span><span style="color:#1F2328;--shiki-dark:#80B9D0">: x[</span><span style="color:#0550AE;--shiki-dark:#D8B56D">1</span><span style="color:#1F2328;--shiki-dark:#80B9D0">])</span></span></code></pre>
<p>Here is the code as one big block:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="python"><code><span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Import fuzzy matching libraries</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">from</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> thefuzz </span><span style="color:#CF222E;--shiki-dark:#C792B8">import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> process</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Load the two dataframes you want to fuzzy match</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">df_1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> pd</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">read_excel(</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'file_1.xlsx'</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">df_2 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> pd</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">read_excel(</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'file_2.xlsx'</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># The column name to match in df_1</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">col_1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> 'column'</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># The column name to match in df_2</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">col_2 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> 'column'</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Create column of match result object. The extractOne function only returns the best match.</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match Result'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">] </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> df_1[col_1]</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">apply(</span><span style="color:#CF222E;--shiki-dark:#C792B8">lambda</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> x</span><span style="color:#1F2328;--shiki-dark:#80B9D0">: process</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">extractOne(x</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> df_2[col_2]))</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Extract fuzzy matching string from result</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">] </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match Result'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">]</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">apply(</span><span style="color:#CF222E;--shiki-dark:#C792B8">lambda</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> x</span><span style="color:#1F2328;--shiki-dark:#80B9D0">: x[</span><span style="color:#0550AE;--shiki-dark:#D8B56D">0</span><span style="color:#1F2328;--shiki-dark:#80B9D0">])</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Extract fuzzy matching score</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match Score'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">] </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> df_1[</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'Fuzzy Match Result'</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">]</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">apply(</span><span style="color:#CF222E;--shiki-dark:#C792B8">lambda</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> x</span><span style="color:#1F2328;--shiki-dark:#80B9D0">: x[</span><span style="color:#0550AE;--shiki-dark:#D8B56D">1</span><span style="color:#1F2328;--shiki-dark:#80B9D0">])</span></span></code></pre>]]></content><author><name>Victor Angelo Blancada</name></author><category term="python" /><category term="data science" /><category term="data analysis" /><summary type="html">Practical guide to fuzzy string matching in Python with TheFuzz and process.extractOne() for deduplicating names, messy addresses, and categorical data.</summary></entry><entry><title type="html">What The Media Gets Wrong About ChatGPT</title><link href="https://victorangeloblancada.github.io/blog/2023/02/09/media-wrong-chatgpt.html" rel="alternate" type="text/html" title="What The Media Gets Wrong About ChatGPT" /><published>2023-02-09T00:00:00.000Z</published><updated>2023-02-09T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2023/02/09/media-wrong-chatgpt</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2023/02/09/media-wrong-chatgpt.html"><![CDATA[<p>Like most people, I enjoyed testing OpenAI’s ChatGPT and I was also excited to see what Google’s Bard was capable of. Unlike most people though, I have been following the development of text generation from Markov chains to Andrej Karpathy’s blog post on <a href="http://karpathy.github.io/2015/05/21/rnn-effectiveness/">The Unreasonable Effectiveness of Recurrent Neural Networks</a>. This has allowed me to see past certain misconceptions in the media about both ChatGPT and Bard.</p>
<h2 id="chatgpt-is-not-a-search-engine-replacement-yet">ChatGPT Is Not a Search Engine Replacement (Yet)</h2>
<p>ChatGPT is a large language model (LLM) created by OpenAI to provide conversational AI services. It was specifically developed to push the envelope of what is possible with generative natural language processing (NLP) through methods such as generative pre-trained transformers (GPT) and reinforcement learning with human feedback (RLHF). Its positioning as a demonstration of cutting-edge NLP capabilities is apparent in OpenAI’s decision to restrict the model’s access to the Internet. ChatGPT instead extrapolates from its finite but admitted very large training data which was limited to only 2021; so if you ask it about events in 2023, it simply extrapolates believable responses based on its 2021 data. This is something ChatGPT does incredibly well: when asked about the version of PyTorch (a widely-used machine learning framework), it gives a response that is reasonably (and believably) close.</p>
<p>Therein lies the rub. The reason ChatGPT’s responses read as very believable because it was designed to maximize its chances of passing the Turing test, i.e. roleplay as a believable human, rather than be correct all the time. Users without an understanding of what ChatGPT was designed for and how it was trained have arrived at the incorrect impression that it can serve as a replacement for search engines. ChatGPT was never designed for that use case and it is, at the very least, unfair to its developers to measure its capabilities as a search engine and, at the very worst, irresponsible and dangerous to use its outputs without first doing some fact-checking.</p>
<p>Why then is Microsoft touting ChatGPT-powered capabilities in Bing? The short answer (believable but not 100% accurate) is that search engines represent an established business model where advertisers would happily pay top dollar to improve brands’ positioning on search results. The longer and more accurate answer is that Microsoft is in fact incorporating ChatGPT into most of its products, including Microsoft Word and Microsoft Teams. The media has simply latched onto the story of ChatGPT-powered Bing because:</p>
<ol>
<li>it represents a bold encroachment by Microsoft upon the search engine space which for decades has been Google’s turf; and</li>
<li>Bing is a free service unlike licensed software such as Microsoft Office, making it very easy for users to test it.</li>
</ol>
<p>On a side note, OpenAI is reportedly working on a the next version of its GPT-powered LLM. With Microsoft’s significant investment in the company, it is not unlikely that this new version will be designed specifically for use as part of the Bing search engine.</p>
<p>That said, Google’s release of Bard in reaction to ChatGPT has (perhaps unfairly) been labeled by the press as underwhelming. Any product that is shoehorned into a use case it wasn’t initially designed for (e.g., an LLM being used as a search engine) in reaction to another existing product is unlikely to succeed. The media’s complaints about bard revolve around two main things:</p>
<ol>
<li>Bard allegedly provides false information; and</li>
<li>Bard’s user experience is supposedly boring</li>
</ol>
<p>The counter-argument to the first point is the same one against the use of ChatGPT as a search engine. At best these LLMs need to balance maximizing accuracy and believability and, at worst, may not emphasize accuracy at all. ChatGPT is supposedly version 3.5 of OpenAI’s GPT-powered LLMs (after GPT, GPT-2, and GPT-3) and has been fine-tuned based on years of human feedback. It is understandable that Bard will need time to reach that level of refinement.</p>
<p>As for the second point, user experience is another thing that improves over time as companies get more feedback from users. It can be argued that among the many breakthroughs of ChatGPT was finding a user-friendly interface for testers and eventual users to enjoy using it. Previous versions of OpenAI’s GPT-powered LLMs (several of which I also tested and even deployed on my own machine) were designed to complete user provided prompts instead of replying as a chatbot. By framing the ChatGPT as a chatbot instead of a text-generation model, OpenAI succeeded in finding a user experience that delighted users instead.</p>
<hr>
<h2 id="ps-openai-is-not-a-small-startup">P.S.: OpenAI Is Not a “Small Startup”</h2>
<p>There have also been numerous stories circulating presenting OpenAI as a tiny startup that beat the behemoth that is Google in some sort of David versus Goliath tale. While this certainly makes a clickbaity (and believable) headline, this isn’t 100% true either.</p>
<p>While OpenAI was originally a non-profit with a small team, it boasts both:</p>
<ol>
<li>financial backing (at various times) from giants such as Microsoft, Amazon Web Services, Peter Thiel, and Elon Musk; and</li>
<li>a highly-regarded and well-compensated research team including OpenAI co-founder and former Google machine learning expert Ilya Sutskever.</li>
</ol>
<p>In an extremely technical field such as machine learning, team size doesn’t affect results as much as individuals’ expertise. It is therefore come as no surprise that the OpenAI team can produce such impressive models despite being depicted by the media as a minnow compared to Google.</p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="nlp" /><category term="llm" /><category term="openai" /><category term="chatgpt" /><category term="google" /><category term="microsoft" /><category term="python" /><summary type="html">OpenAI's ChatGPT is a large language model designed to provide conversational AI services, but it is not yet a replacement for search engines.</summary></entry><entry><title type="html">Marketing Mix Modeling for Engineers</title><link href="https://victorangeloblancada.github.io/blog/2023/01/10/mmm-for-engineers.html" rel="alternate" type="text/html" title="Marketing Mix Modeling for Engineers" /><published>2023-01-10T00:00:00.000Z</published><updated>2023-01-10T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2023/01/10/mmm-for-engineers</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2023/01/10/mmm-for-engineers.html"><![CDATA[<p>Marketing Mix Modeling (MMM) is a powerful tool for understanding the effectiveness of a company’s marketing campaigns and strategies. Over the past decade, Python has become a popular choice for engineers and data scientists alike who are looking to develop and build MMM models. In this guide, we will discuss the fundamentals of marketing mix modeling and how to implement it using Python.</p>
<h2 id="data-collection">Data Collection</h2>
<p>The success of any MMM model is dependent on the data that is used to build it. It is important to collect the right data that will provide the insights and results needed. This includes data such as customer demographics, sales data, media spend, and other factors that could have an effect on the results. Once the data is gathered, it needs to be cleaned, organized, and prepared for analysis.</p>
<h2 id="selecting-features-for-the-regression-equation">Selecting Features for the Regression Equation</h2>
<p>When building a marketing mix model, the choice of features is critical for achieving accurate results. Here, we will focus on selecting the appropriate features for the regression equation.</p>
<h3 id="marketing-features">Marketing Features</h3>
<p>The most obvious starting point is to include all relevant marketing features. This includes, but is not limited to:</p>
<ul>
<li>TV Spend</li>
<li>Digital Impressions</li>
<li>Print/Out of Home Advertising</li>
<li>Social Media Metrics</li>
<li>Other Promotional Activity</li>
</ul>
<h3 id="non-marketing-features">Non-Marketing Features</h3>
<p>In addition to marketing metrics, there are also non-marketing features that should be considered. These include:</p>
<ul>
<li>Price of the Product</li>
<li>Seasonality</li>
<li>Competitor Activity</li>
</ul>
<h3 id="further-feature-engineering">Further Feature Engineering</h3>
<p>After selecting the features, further feature engineering may be necessary. This includes:</p>
<ul>
<li>Normalizing Values</li>
<li>Removing Outliers</li>
</ul>
<p>Using the right combination of features, marketing mix models can be incredibly powerful tools for understanding the impact of marketing activities on sales.</p>
<h2 id="model-building">Model Building</h2>
<p>Once the data is prepared, engineers can begin building the MMM model. This involves using Python’s various libraries such as Scikit-Learn and Pandas to build and test the model. The model should be tested multiple times to ensure that the results are accurate and reliable.</p>
<h2 id="mathematical-transformations">Mathematical Transformations</h2>
<p>Mathematical transformations are used in marketing mix modeling to analyze data and gain insights. Two of the most common transformations are decays or ad stocks and response curves. Both of these transformations are useful for gaining insights into the effectiveness of marketing campaigns.</p>
<h3 id="decays-or-ad-stocks">Decays or Ad Stocks</h3>
<p>Decays or ad stocks are a type of marketing tactic used to track and measure the decline in the effectiveness of a promotional campaign over a period of time. This decline, known as the decay rate, is evaluated to determine the rate at which the campaign’s influence wanes; this is important information for marketers, as it helps inform decisions as to when to refresh a campaign or create new ones. By monitoring the decay rate, marketers can ensure that their campaigns remain effective and that they are able to adjust their strategies accordingly.</p>
<h3 id="response-curves">Response Curves</h3>
<p>Response curves are a useful tool for marketers looking to understand the relationship between a marketing metric and sales. They can be used to estimate the impact of a marketing campaign by plotting the relationship between a metric and sales. Generally, sales response curves downward as diminishing returns set in. Plotting the response curve allows marketers to determine the potential impact of a campaign and adjust their strategy accordingly. It also helps them identify opportunities for improvement and inform decision-making around marketing strategies and investments.</p>
<h2 id="iterating-models">Iterating Models</h2>
<p>Analysts need to iterate through multiple possible marketing mix models to identify the equation that will provide the best estimate of the impact of marketing on sales. This process is usually achieved by performing a grid search across all potential models, based on a set of assumptions regarding the variables involved and the mathematical transformations that need to be applied.</p>
<p>The model with the highest accuracy – as measured using error metrics such as MSE, MAPE or coefficients of determination R2 – is often preferred. However, it is also important to ensure that the model’s coefficients make sense, and that the model is able to perform well, even when applied to data outside of the sample range. This prevents analysts from selecting models with nonsensical coefficients, such as an inverse relationship between media spending and sales.</p>
<h2 id="analysis-and-results">Analysis and Results</h2>
<p>Once the MMM model is built and tested, engineers can begin analyzing the results. The results should be compared to the objectives of the project and used to gain insights into the effectiveness of the marketing campaigns and strategies.</p>
<h2 id="conclusion">Conclusion</h2>
<p>In conclusion, marketing mix modeling is a powerful tool for understanding the effectiveness of a company’s marketing campaigns and strategies. Python is a great choice for engineers and data scientists who are looking to build and test MMM models. With the right data, model building, and analysis, engineers can gain valuable insights into their marketing efforts.</p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="ai" /><category term="ml" /><category term="artificial intelligence" /><category term="machine learning" /><category term="marketing" /><category term="marketing mix modeling" /><category term="python" /><summary type="html">Marketing Mix Modeling (MMM) is a powerful tool for understanding the effectiveness of a company’s marketing campaigns and strategies.</summary></entry><entry><title type="html">Cropping an MS Teams Recording</title><link href="https://victorangeloblancada.github.io/blog/2022/12/07/teams-recording-resize.html" rel="alternate" type="text/html" title="Cropping an MS Teams Recording" /><published>2022-12-07T00:00:00.000Z</published><updated>2022-12-07T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2022/12/07/teams-recording-resize</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2022/12/07/teams-recording-resize.html"><![CDATA[<p><em>This is part of a series of short blog posts about automating <s>boring</s> repetitive work using Python.</em></p>
<p>Have you ever saved a recording of an MS Teams call where someone shared their screen and wanted to crop the recording to only keep the screen share? If so, you’re in luck.</p>
<p>This solution requires the <strong>ffmpeg</strong> library which I previously discussed as part of <a href="/blog/2019/09/20/command-line-gifs.html">a post covering creating GIFs from the command line</a>.</p>
<p>Running the following code in the command line automatically crops the MS Teams recording named “path_to_recording.mp4” and saves the output to “output.mp4”.</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="plaintext"><code><span class="line"><span>ffmpeg -i "path_to_recording.mp4" -filter:v "crop=1725:970:98:0" output.mp4</span></span></code></pre>
<p>Before:</p>
<p><img src="/assets/images/Teams_Sample_Before.jpg" alt="Original recording"></p>
<p>After:</p>
<p><img src="/assets/images/Teams_Sample_After.jpg" alt="Cropped recording"></p>
<p>I do acknowledge that this solution depends on how MS Teams orients its interface and your screen resolution, but even if this changes, the general principle stays the same. You only need to alter the <code>crop=1725:970:98:0</code> part of the code. This section uses the following order of arguments: <code>crop=out_width:out_height:top_left_corner_x:top_left_corner_y</code>. So if you want to keep a video of width 1920 and height 1080 where the top left corner is at (0,0), the code would instead be:</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="plaintext"><code><span class="line"><span>ffmpeg -i "path_to_recording.mp4" -filter:v "crop=1920:1080:0:0" output.mp4</span></span></code></pre>]]></content><author><name>Victor Angelo Blancada</name></author><category term="blog" /><category term="python" /><category term="video processing" /><category term="coding" /><category term="automation" /><summary type="html">How to crop a Microsoft Teams screen recording MP4 in one line with FFmpeg (-filter:v crop) to isolate the shared screen and remove webcam panels.</summary></entry><entry><title type="html">NaNoGenMo 2022</title><link href="https://victorangeloblancada.github.io/blog/2022/12/01/nanogenmo-2022.html" rel="alternate" type="text/html" title="NaNoGenMo 2022" /><published>2022-12-01T00:00:00.000Z</published><updated>2022-12-01T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2022/12/01/nanogenmo-2022</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2022/12/01/nanogenmo-2022.html"><![CDATA[<p>I participated in <a href="https://github.com/NaNoGenMo/2022">National Novel Generation Month for 2022</a>. This marked the third consecutive year where I completed my project.  <a href="https://github.com/victorangeloblancada/nanogenmo-2022">See here</a> for my GitHub repository with my code and my final novel.</p>
<p>With the recent breakthroughs in generative AI, I decided it was high time for me to use a Large Language Model (LLM) for a NaNoGenMo project. I opted to fine-tune a <a href="https://www.eleuther.ai/projects/gpt-neo/">GPT-Neo model</a> as I wanted control over the model itself lest the project turn into a prompt engineering exercise. I used <a href="https://github.com/minimaxir">Max Woolf</a>’s <a href="https://github.com/minimaxir/aitextgen">aitextgen</a> package to finetune the 125M GPT-Neo model on <a href="https://github.com/victorangeloblancada/nanogenmo-2022/tree/main/corpus">philosophy texts by Aristotle, Plato, Kant, Hume, and Nietzsche</a>. I used <a href="https://github.com/aparrish/pytracery">pytracery</a> to define grammars for model prompts.</p>
<p>Here are some brief, cherry-picked snippets from the output:</p>
<p><strong>What Will Happen If Literature Is Divine?</strong></p>
<blockquote>
<p>What will happen if literature is divine?</p>
<p>CLEINIAS: What do you mean? he said.</p>
<p>ATHENIAN: I should say, that it must be the greatest of all things which can be known to us; and we ought not to seek for them in order that they may know what sort of things are better than words—and now let us have a way out of them.</p>
<p>CLEINIAS: That would be my desire.</p>
<p>ATHENIAN: But there is no difficulty in seeing how wisely Hesiod spoke when he says ‘the truth.’</p>
<p>MEGILLUS: True.</p>
</blockquote>
<p><strong>What Is A Lover?</strong></p>
<blockquote>
<p>What is a lover?</p>
<p>Yes, he said.</p>
<p>And what do you say of him who loves no one but himself? Would you not admit him to be loved by any one in whom there is no reason for praising or blaming others? For pity and hatred are opposites; they cannot be compared with each other when the latter becomes more innocent than the former.</p>
</blockquote>
<p><strong>What Is The Best Way To Be Joyful?</strong></p>
<blockquote>
<p>What is the best way to be joyful?</p>
<p>CLEINIAS: Certainly not, Stranger.</p>
<p>ATHENIAN: And when you say that any one should abstain from weeping and lamenting in your sorrows utterances—whence do you admit this?</p>
</blockquote>
<p><em>Victor Blancada is a data scientist. Visit his LinkedIn page <a href="https://www.linkedin.com/in/geloblancada/">here</a>.</em></p>]]></content><author><name>Victor Angelo Blancada</name></author><category term="blog" /><category term="nlp" /><category term="nanogenmo" /></entry><entry><title type="html">Resize an Image to 4R</title><link href="https://victorangeloblancada.github.io/blog/2022/09/21/image-resize.html" rel="alternate" type="text/html" title="Resize an Image to 4R" /><published>2022-09-21T00:00:00.000Z</published><updated>2022-09-21T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2022/09/21/image-resize</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2022/09/21/image-resize.html"><![CDATA[<p><em>This is part of a series of short blog posts about automating <s>boring</s> repetitive work using Python.</em></p>
<p>The following code automatically resizes all images in the directory specified in <code>mypath</code> to 4R aspect ratio. 4R images have a size of 4 inches by 6 inches (or 10.2 cm by 15.2 cm), which means that images must have a 2 by 3 aspect ratio.</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="python"><code><span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Import libraries</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">from</span><span style="color:#0550AE;--shiki-dark:#E6E0D4"> PIL</span><span style="color:#CF222E;--shiki-dark:#C792B8"> import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> Image</span></span>
<span class="line"></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">from</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> os </span><span style="color:#CF222E;--shiki-dark:#C792B8">import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> listdir</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">from</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> os</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">path </span><span style="color:#CF222E;--shiki-dark:#C792B8">import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> isfile</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> join</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Directory containing the images</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">mypath </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> 'Photos For Printing'</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">d1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 4</span><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"> # First dimension</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">d2 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 6</span><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"> # Second dimension</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Resize mode: Crop or extend with white space?</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">#mode = 'Crop'</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">mode </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> 'Extend'</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">files </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [f </span><span style="color:#CF222E;--shiki-dark:#C792B8">for</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> f </span><span style="color:#CF222E;--shiki-dark:#C792B8">in</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> listdir(mypath)</span><span style="color:#CF222E;--shiki-dark:#C792B8"> if</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> isfile(join(mypath</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> f))</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">for</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> myfile </span><span style="color:#CF222E;--shiki-dark:#C792B8">in</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> files</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Opens a image in RGB mode</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    im </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> Image</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">open(mypath</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'/'</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#1F2328;--shiki-dark:#80B9D0">myfile)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Process image</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> mode </span><span style="color:#CF222E;--shiki-dark:#D58A6A">==</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> 'Crop'</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">        if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">&#x3C;</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">            if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">></span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">                # Setting the points for cropped image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                left </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                top </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> h) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                right </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                bottom </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( m</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> h) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">            else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">                # Setting the points for cropped image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                left </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> w) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                top </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                right </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> w) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                bottom </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">        else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">            if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">></span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">                # Setting the points for cropped image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                left </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> w) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                top </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                right </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> w) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                bottom </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">            else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">                # Setting the points for cropped image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                left </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                top </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> h) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                right </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                bottom </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( m</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> h) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">        if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">&#x3C;</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">            if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">></span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">                # Setting the points for extended image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                left </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                top </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> h) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                right </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                bottom </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> h) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">            else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">                # Setting the points for extended image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                left </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> w) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                top </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                right </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> w) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                bottom </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">        else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">            w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">            if</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">></span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">                # Setting the points for extended image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                left </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> w) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                top </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                right </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> w) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                bottom </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">height         </span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">            else</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                w </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                h </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width </span><span style="color:#CF222E;--shiki-dark:#D58A6A">*</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d2 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> d1</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">                # Setting the points for extended image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                left </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                top </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> h) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                right </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">width</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">                bottom </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0550AE;--shiki-dark:#80B9D0"> int</span><span style="color:#1F2328;--shiki-dark:#80B9D0">( im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> h) </span><span style="color:#CF222E;--shiki-dark:#D58A6A">/</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 2</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> )</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Cropped image of above dimension</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    im1 </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> Image</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">new(</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'RGB'</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (right </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> left</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> bottom </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> top)</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (</span><span style="color:#0550AE;--shiki-dark:#D8B56D">255</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 255</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 255</span><span style="color:#1F2328;--shiki-dark:#80B9D0">))</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    im1</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">paste(im</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (</span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0">left</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#CF222E;--shiki-dark:#D58A6A"> -</span><span style="color:#1F2328;--shiki-dark:#80B9D0">top))</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Save image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    im1</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">save(mypath</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'/'</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#1F2328;--shiki-dark:#80B9D0">myfile)</span></span>
<span class="line"></span></code></pre>]]></content><author><name>Victor Angelo Blancada</name></author><category term="blog" /><category term="python" /><category term="image processing" /><category term="coding" /><category term="automation" /></entry><entry><title type="html">Adding a Date Watermark to an Image</title><link href="https://victorangeloblancada.github.io/blog/2022/09/20/image-watermark.html" rel="alternate" type="text/html" title="Adding a Date Watermark to an Image" /><published>2022-09-20T00:00:00.000Z</published><updated>2022-09-20T00:00:00.000Z</updated><id>https://victorangeloblancada.github.io/blog/2022/09/20/image-watermark</id><content type="html" xml:base="https://victorangeloblancada.github.io/blog/2022/09/20/image-watermark.html"><![CDATA[<p><em>This is part of a series of short blog posts about automating <s>boring</s> repetitive work using Python.</em></p>
<p>The following code automatically adds a date watermark to all images in the directory specified in <code>mypath</code>.</p>
<pre class="astro-code astro-code-themes github-light-default lab-dark" style="background-color:#ffffff;--shiki-dark-bg:#1B3040;color:#1f2328;--shiki-dark:#E6E0D4; overflow-x: auto;" tabindex="0" data-language="python"><code><span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Import libraries</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">from</span><span style="color:#0550AE;--shiki-dark:#E6E0D4"> PIL</span><span style="color:#CF222E;--shiki-dark:#C792B8"> import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> Image</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> ImageFont</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> ImageDraw</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">from</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> os </span><span style="color:#CF222E;--shiki-dark:#C792B8">import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> listdir</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">from</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> os</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">path </span><span style="color:#CF222E;--shiki-dark:#C792B8">import</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> isfile</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> join</span></span>
<span class="line"></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">def</span><span style="color:#8250DF;--shiki-dark:#80B9D0"> get_date_taken</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">(path):</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    """Function to get the date taken EXIF data.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    path: File path of image.</span></span>
<span class="line"><span style="color:#0A3069;--shiki-dark:#A9B98C">    """</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">    return</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> Image</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">open(path)</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">_getexif()</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">[</span><span style="color:#0550AE;--shiki-dark:#D8B56D">36867</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Specify directory</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">mypath </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#0A3069;--shiki-dark:#A9B98C"> 'Photos-001'</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Get list of files in the specified directory</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">onlyfiles </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> [f </span><span style="color:#CF222E;--shiki-dark:#C792B8">for</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> f </span><span style="color:#CF222E;--shiki-dark:#C792B8">in</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> listdir(mypath)</span><span style="color:#CF222E;--shiki-dark:#C792B8"> if</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> isfile(join(mypath</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> f))</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic"># Loop through list of files</span></span>
<span class="line"><span style="color:#CF222E;--shiki-dark:#C792B8">for</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> image_fn </span><span style="color:#CF222E;--shiki-dark:#C792B8">in</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> onlyfiles</span><span style="color:#1F2328;--shiki-dark:#D58A6A">:</span></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Open image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    watermark_image </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> Image</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">open(mypath</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'/'</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#1F2328;--shiki-dark:#80B9D0">image_fn)</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Get image dimensions</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    width</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> watermark_image</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#E6E0D4">size</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Initialize ImageDraw</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    draw </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> ImageDraw</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">Draw(watermark_image)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Set font size to around 3% of height (height divided by 33)</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    font </span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#E6E0D4"> ImageFont</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">truetype(</span><span style="color:#0A3069;--shiki-dark:#A9B98C">"arial.ttf"</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> height</span><span style="color:#CF222E;--shiki-dark:#D58A6A">//</span><span style="color:#0550AE;--shiki-dark:#D8B56D">33</span><span style="color:#1F2328;--shiki-dark:#80B9D0">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Add watermark</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    draw</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">text((</span><span style="color:#0550AE;--shiki-dark:#D8B56D">0</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> height </span><span style="color:#CF222E;--shiki-dark:#D58A6A">-</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> (height</span><span style="color:#CF222E;--shiki-dark:#D58A6A">//</span><span style="color:#0550AE;--shiki-dark:#D8B56D">33</span><span style="color:#1F2328;--shiki-dark:#80B9D0">))</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> </span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#80B9D0">              get_date_taken(mypath</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#0A3069;--shiki-dark:#A9B98C">'/'</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#1F2328;--shiki-dark:#80B9D0">image_fn)</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#1F2328;--shiki-dark:#80B9D0"> </span></span>
<span class="line"><span style="color:#953800;--shiki-dark:#E6E0D4">              font</span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#80B9D0">font</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span></span>
<span class="line"><span style="color:#953800;--shiki-dark:#E6E0D4">              fill</span><span style="color:#CF222E;--shiki-dark:#D58A6A">=</span><span style="color:#1F2328;--shiki-dark:#80B9D0">(</span><span style="color:#0550AE;--shiki-dark:#D8B56D">255</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 165</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 0</span><span style="color:#1F2328;--shiki-dark:#D58A6A">,</span><span style="color:#0550AE;--shiki-dark:#D8B56D"> 255</span><span style="color:#1F2328;--shiki-dark:#80B9D0">))</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6E7781;--shiki-light-font-style:inherit;--shiki-dark:#81969A;--shiki-dark-font-style:italic">    # Save image</span></span>
<span class="line"><span style="color:#1F2328;--shiki-dark:#E6E0D4">    watermark_image</span><span style="color:#1F2328;--shiki-dark:#D58A6A">.</span><span style="color:#1F2328;--shiki-dark:#80B9D0">save(mypath</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#0A3069;--shiki-dark:#A9B98C">' Labeled/'</span><span style="color:#CF222E;--shiki-dark:#D58A6A">+</span><span style="color:#1F2328;--shiki-dark:#80B9D0">image_fn)</span></span></code></pre>]]></content><author><name>Victor Angelo Blancada</name></author><category term="blog" /><category term="python" /><category term="image processing" /><category term="coding" /><category term="automation" /></entry></feed>
