The AI Flywheel: How Agents Learn from Production

Peter Bubenik · Shopify ML · · Source
Image for Sidekick's continual learning loop (2026) - Shopify

After studying this material, you should be able to:

  1. Explain why frontier models alone are insufficient for production AI at scale
  2. Describe each stage of the continual learning flywheel and how they connect
  3. Apply the quality definition and judge calibration process to a real AI product
  4. Understand how production failures become training signal through self-healing pipelines
  5. Explain how gist compression reduces latency and cost without sacrificing quality
  6. Evaluate the tradeoffs between frontier models and fine-tuned specialized models

Step-by-Step Study Material

Step 1: Understanding the Core Problem

Why Frontier Models Are Not Enough Long-Term

Think of a frontier model like a brilliant new hire on their first day. They are highly capable and broadly knowledgeable, but they have never seen your specific customers, your specific failures, or your specific edge cases.

Frontier Model Limitations:
┌─────────────────────────────────────────────┐
│  ✗ Expensive at scale                       │
│  ✗ Slower than specialized alternatives     │
│  ✗ General-purpose, not product-specific    │
│  ✗ Frozen — does not learn from production  │
│  ✗ Failures pile up as prompts/code, not    │
│    as improved model weights                │
└─────────────────────────────────────────────┘

The Key Insight

Every production failure is hard-won knowledge. The question is: where does that knowledge live?

Without a flywheel:

User failure → Bug report → Slack thread → Prompt edit → Still frozen model

With a flywheel:

User failure → Training signal → Better model weights → Fewer future failures

Knowledge accumulates in continuous parameter space (model weights), not just in discrete artifacts (prompts, rules, code).


Step 2: The Flywheel Overview

The continual learning loop has five major stages. Think of it as a cycle, not a one-time process.

┌──────────────────────────────────────────────────────┐
│              THE CONTINUAL LEARNING FLYWHEEL         │
│                                                      │
│   1. Define Quality (Rubric + Ground Truth)          │
│           ↓                                          │
│   2. Calibrate Your Judge (Automated Evaluator)      │
│           ↓                                          │
│   3. Improve Frontier Baseline (Autoresearch)        │
│           ↓                                          │
│   4. Self-Healing Pipeline (Failures → Training)     │
│           ↓                                          │
│   5. Compress for Speed (Gist Tokens)                │
│           ↓                                          │
│   [Loop back — better model enters next cycle]       │
└──────────────────────────────────────────────────────┘

Step 3: Stage 1 — Defining Quality

Why This Is the Most Critical Step

If you define quality incorrectly, everything downstream optimizes the wrong behavior. This is like setting the wrong destination in a GPS — the system will efficiently take you somewhere you do not want to go.

The Rubric

A rubric translates product requirements into scored, concrete criteria.

Example rubric structure:

┌──────────────────┬────────────────────────────────────────┐
│ Criterion        │ What it measures                       │
├──────────────────┼────────────────────────────────────────┤
│ Completeness     │ Did the response address all parts     │
│                  │ of the user's request?                 │
├──────────────────┼────────────────────────────────────────┤
│ Execution        │ Did the agent take the right actions?  │
├──────────────────┼────────────────────────────────────────┤
│ Response Quality │ Was the output clear and accurate?     │
├──────────────────┼────────────────────────────────────────┤
│ Safety           │ Did it avoid harmful outputs?          │
└──────────────────┴────────────────────────────────────────┘

Each score level needs concrete anchors — not vague descriptions, but specific examples of what a 1, 3, or 5 looks like.

Ground Truth Data

⚠️ Critical rule: Include randomly sampled traffic, not only curated examples.

Data TypeWhat It TestsRisk If Overused
Golden/curated setsCases you already know aboutBlind to unknown failure modes
Random production samplesWhat actually happens in the wildNone — this is the goal

Inter-Annotator Agreement (IAA)

Before trusting your rubric, test it with two expert annotators scoring the same 25 samples independently.

Measurement: Cohen's Kappa (κ)

κ measures agreement ABOVE chance level

κ ≈ 0.2  →  Rubric is ambiguous. Stop. Revise it.
κ ≈ 0.6  →  Moderate agreement. Acceptable to proceed.
κ ≈ 0.8+ →  Strong agreement. Rubric is clear.

Key principle:

If expert humans who work on this product every day cannot agree, an LLM judge will not agree either.

The IAA score becomes the ceiling for your automated judge. You are not aiming for a perfect judge — you are aiming for a judge that matches humans about as well as humans match each other.

What Good Annotations Look Like

❌ Bad annotation:
   Score: 3
   "Response was okay."

✅ Good annotation:
   Score: 3
   "The agent correctly identified the low-stock products
   but failed to sort them by urgency as the merchant
   implied. The query ran successfully but the response
   omitted the variant-level detail the merchant needed
   to act on the information."

The reasoning behind every score is what calibration algorithms learn from.


Step 4: Stage 2 — Calibrating the Judge

What Calibration Does

The rubric is just a starting prompt. Calibration transforms it into a judge that can reliably score millions of production conversations the way your best human experts would.

Rubric (starting prompt)
        ↓
  + Ground truth annotations
        ↓
  + Calibration optimizer (e.g., DSPy + GEPA or ACE)
        ↓
  Calibrated Judge (automated evaluator)

Calibration Tools

GEPA (Reflective Prompt Evolution):

  • Reflects on natural-language failure traces
  • Evolves the prompt iteratively
  • Keeps a Pareto frontier of candidates rather than greedily picking one winner
  • Avoids getting stuck in local optima

ACE (Agentic Context Engineering):

  • Builds a structured playbook through small, incremental edits
  • More conservative, step-by-step improvement

Validating the Judge

Your judge is an offline proxy metric. Before trusting it, you must verify it reflects real-world performance.

Validation method 1 — Backtesting:

Take previous A/B tests where you know the outcome
→ Run the judge on both variants
→ Does the judge correctly identify which variant won?

Validation method 2 — Degradation testing:

Deliberately make one specific behavior worse
→ Confirm the corresponding judge criterion drops
→ Confirm other criteria are NOT affected

Example:
  Disable goal-fulfillment behavior
  → Goal-fulfillment score should fall
  → Safety score should remain unchanged

Judge Design Principle

Keep each judge small and targeted. One focused judge per behavior is easier to validate and easier to trust than one giant judge trying to measure everything.


Step 5: Stage 3 — Improving the Frontier Baseline (Autoresearch)

What This Stage Accomplishes

Before touching model weights, you push the existing frontier-powered system as far as it can go through automated optimization of the harness — the prompts, tool definitions, and orchestration code.

Why Simple Prompt Tuning Is Not Enough

A production AI application is not a single prompt. It has:

  • Dynamically assembled prompts
  • Custom control loops
  • Tool definitions
  • Orchestration code spread across a large codebase

Tuning one prompt touches only a small fraction of the system's behavior.

The Autoresearch Loop

┌─────────────────────────────────────────────────┐
│              AUTORESEARCH LOOP                  │
│                                                 │
│  Agent proposes change                          │
│  (prompt / tool definition / harness code)      │
│           ↓                                     │
│  Evaluate against calibrated judge              │
│           ↓                                     │
│  Score improved? → Keep change                  │
│  Score same/worse? → Discard change             │
│           ↓                                     │
│  Repeat until improvements plateau              │
└─────────────────────────────────────────────────┘

The entire configuration lives in one readable markdown file specifying:

  • Where to get data
  • Which directories the agent may edit
  • Which judge to use as the metric
  • Which optimizer to use
  • The propose-evaluate-keep-or-discard loop

Step 6: Stage 4 — Self-Healing Pipeline (Failures → Weights)

The Transition: Discrete → Continuous

Once harness improvements plateau, you move from optimizing words and code to optimizing model weights. This is the heart of the flywheel.

Discrete artifacts (prompts, rules, code)
         ↓ plateau
Continuous parameter space (model weights)

Mining Hard Negatives

The pipeline starts by identifying hard negatives: production conversations the judge scores low. These are your richest learning opportunities.

At scale, real traffic naturally produces:

  • Partial context situations
  • Ambiguous requests
  • Business-specific workflows
  • Tool failures
  • Many phrasings of the same intent

The Self-Healing Pipeline in Detail

Low-scoring production conversation
              ↓
┌─────────────────────────────────────┐
│  Panel of frontier reasoning models │
│  critiques the failure              │
└─────────────────────────────────────┘
              ↓
┌─────────────────────────────────────┐
│  Arbiter merges critiques into      │
│  a single repair instruction        │
└─────────────────────────────────────┘
              ↓
  Repair instruction injected before
  user's turn ("hinting" technique)
              ↓
  Conversation replayed from that point
              ↓
  Judge scores the replayed conversation
              ↓
    ┌─────────────┴──────────────┐
    ↓                            ↓
 Passes judge               Still fails
    ↓                            ↓
Becomes RL trajectory      Human annotation
with judge score           (Toloka experts)
as reward                  using same rubric

Training: Two Stages

Stage 1 — Supervised Fine-Tuning (SFT):

Input: Healed trajectories (including reasoning, not just final answers)
Goal:  Distill successful behavior into smaller model

Why include reasoning?
→ Chain-of-thought distillation lets the smaller model
  inherit behavior it could not learn from answers alone

Stage 2 — Reinforcement Learning with GRPO:

For each prompt:
  Model generates a GROUP of responses
          ↓
  Judge scores each response
          ↓
  GRPO reinforces the best-scoring responses
          ↓
  Model directly optimizes against your definition of quality

How SFT and GRPO work together:

MethodRole
SFTTeaches the model what successful trajectories look like (imitation)
GRPOOptimizes the model directly against the quality definition (reinforcement)

Preventing Catastrophic Forgetting

The pipeline runs daily, continuously adding new trajectories. Each training cycle trains on both new and previous trajectories to prevent the model from forgetting earlier lessons while learning new ones.

Cycle 1 data: [A, B, C]
Cycle 2 data: [A, B, C, D, E]   ← includes previous
Cycle 3 data: [A, B, C, D, E, F, G]   ← includes all previous

Step 7: Stage 5 — Gist Compression

The Problem: Long Prompts Are Expensive

An agent's system prompt is long and static. Every generated token must attend over the entire prompt.

Attention cost scales with sequence length
→ Long prompt = fixed latency and cost tax on EVERY request

How Gist Compression Works

Teacher model:  [Full system prompt — ~6,000 tokens] → Output
                         ↓ train to match
Student model:  [~1,500 learned gist tokens] → Same output

What is trained: Only the gist token EMBEDDINGS
What is frozen:  The model weights themselves

Gist tokens are learned compressed representations of the full prompt. They are not human-readable — they are vectors in embedding space that reproduce the prompt's behavioral effect.

Results from the GraphQL Agent

System prompt:  ~6,000 tokens
After gisting:  ~1,500 tokens  (75% reduction)

Performance impact at 350 requests/minute:
┌────────────────────────────────┬──────────┐
│ Metric                         │ Change   │
├────────────────────────────────┼──────────┤
│ Time-to-first-token            │ -19%     │
│ End-to-end latency             │ -38%     │
│ Requests per second            │ +16%     │
│ Output tokens per second       │ +12%     │
│ GPUs needed for same traffic   │ -14%     │
│ Quality loss (judge score)     │ None     │
└────────────────────────────────┴──────────┘

Step 8: The Full Picture — GraphQL Agent Results

Putting it all together, here is what the flywheel achieved for Shopify's GraphQL agent serving up to 2,000 requests per minute:

Frontier model baseline
        ↓ flywheel runs
Specialized fine-tuned model

Quality:   Surpasses frontier model
Cost:      ~$27M/year → ~$1M/year  (96% reduction)
Latency:   38% faster end-to-end
Hardware:  ~14% fewer GPUs for same traffic

Why the Cost Reduction Is So Large

Frontier model cost drivers:
- Large model = expensive per token
- Long prompts = many tokens per request
- High volume = costs multiply

Fine-tuned model advantages:
- Smaller model = cheaper per token
- Gist compression = fewer tokens per request
- Same or better quality = no quality tradeoff

Step 9: Conceptual Summary — The Durable Advantage

Traditional approach:
  Launch → Failures → Prompt edits → Still same frozen model
  Knowledge lives in: words and code (discrete artifacts)

Flywheel approach:
  Launch → Failures → Training signal → Better weights
  Knowledge lives in: model parameters (continuous space)
  
  Each cycle begins with a MORE CAPABLE MODEL,
  not merely a more elaborate harness.

The Compounding Effect

Cycle 1: Frontier model + improved harness
Cycle 2: Slightly better model + same harness improvements
Cycle 3: Better model + new production lessons folded in
Cycle N: Specialized model that surpasses frontier baseline
         at a fraction of the cost and latency

Quick Reference: Key Terms

TermDefinition
FlywheelContinual learning loop that converts production experience into model weight updates
Hard negativesProduction conversations the judge scores low; richest training signal
Cohen's KappaMeasure of inter-annotator agreement above chance level
GEPAPrompt optimizer using reflective evolution and Pareto frontier selection
HintingInjecting a repair instruction before the user's turn to guide replay
SFTSupervised Fine-Tuning; teaches model to imitate successful trajectories
GRPOReinforcement learning method that scores groups of responses and reinforces the best
Gist tokensLearned compressed token embeddings that replace a long system prompt
Catastrophic forgettingModel losing earlier knowledge when trained on new data; prevented by including previous trajectories
Discrete artifactsPrompts, rules, code — where knowledge lives without a flywheel
Continuous parameter spaceModel weights — where knowledge lives with a flywheel

More to study