READINGCode Over Context: Why Written Agent Skills Fail in Production (and How to Distill Them into Deterministic Tools)
Agent Architecture8 min read

Code Over Context: Why Written Agent Skills Fail in Production (and How to Distill Them into Deterministic Tools)

Why prompt-heavy agent skills bloat context, drift across turns, and break on light models, and how to architect an Elastic Cognitive Envelope that distills frontier reasoning into deterministic code for fast serverless and on-device runtimes.

Illustration for Code Over Context: Why Written Agent Skills Fail in Production (and How to Distill Them into Deterministic Tools)
AUDIO OVERVIEWFenrir Studio Voice • EBU R128 (-16 LUFS)
NOW PLAYING:The Context Trap & Prompt Bloat
0:00
3:15

Most engineering teams build autonomous AI agents by stuffing markdown prompt files into the context window. At ten skills, an agent burns 20,000 tokens per turn before the user types a single character. Worse, probabilistic instructions drift across multi-turn sessions and completely collapse when executed on light or on-device models.

Prompt-heavy skills treat the language model as both the reasoning engine and the deterministic runtime. When you ask a probabilistic model to execute state machines, validate schemas, or calculate token quotas through natural language instructions, you pay maximum latency and cost for minimum reliability.

The Core Thesis: Stop writing prompts for what code can guarantee. Use frontier models to explore and distill complex workflows into deterministic code-first tools, then execute those tools with sub-millisecond latency on fast serverless and on-device runtimes.
PART 01

The context trap and model fragility

01

The prompt bloat dilemma

As agent capabilities expand, developers accumulate markdown instruction files describing CLI flags, formatting rules, and error recovery procedures. In a typical agent harness, these files are injected into the system prompt on every turn.

This practice introduces three severe production bottlenecks:

  • Compounding Token Tax: Loading 15,000 to 20,000 tokens of markdown instructions across a 20-turn session consumes 300,000 to 400,000 input tokens, inflating API billing linearly with conversation length.
  • Attention Degradation: As context length grows, models suffer from needle-in-a-haystack attention decay, missing critical constraints buried in middle paragraphs.
  • Non-Deterministic Drift: Natural language instructions are probabilistic suggestions. Models occasionally skip validation steps, hallucinate non-existent CLI parameters, or format outputs inconsistently.

This failure mode is well-documented in tool-use literature. As demonstrated in ToolLLM (Qin et al., 2023), stuffing raw documentation into prompts causes severe parameter hallucination, whereas distilling capabilities into structured tool contracts restores execution fidelity.

02

Why written skills fail on light models

Written skills create an invisible dependency on massive frontier models. Frontier reasoning models possess sufficient cognitive capacity to follow complex multi-step instructions despite prompt ambiguity.

However, when you attempt to deploy the same agent across lighter runtimes, the system breaks down in distinct ways:

  • On-Device Runtimes: Local mobile and browser models operate under constrained context windows (often 2K to 8K tokens) and limited compute budgets. They cannot reliably ingest ten pages of markdown rules while maintaining conversational state.
  • Fast Serverless Endpoints: While lightweight cloud models offer expansive context windows, stuffing them with markdown skills triggers attention degradation, inflates per-turn latency, and multiplies token costs across multi-turn sessions.

If your agent architecture requires a frontier model just to parse a date or validate a JSON payload, your system is economically and architecturally fragile.

PART 02

The elastic cognitive envelope

03

The exoskeleton vs. the brain

To build resilient agents that operate across model tiers, decouple the cognitive reasoning layer from the deterministic execution layer. We formalize this separation as the Elastic Cognitive Envelope:

  • The Brain (Probabilistic Reasoning): Intent classification, ambiguous user goal decomposition, creative synthesis, and high-level strategy. This layer belongs in the model.
  • The Exoskeleton (Deterministic Code): State machines, schema validation, arithmetic calculations, API authentication, and file system mutations. This layer belongs in compiled or interpreted code.

Deterministic code eliminates prompt token bloat, executes in milliseconds, and enables standard unit test coverage. By moving operational invariants into code, the prompt shrinks from thousands of lines to a concise tool schema.

Depending on security, compute, and platform constraints, teams deploy this deterministic exoskeleton across several architectural topologies:

  • In-Process Execution: In local developer tooling and CLI agents, the exoskeleton runs directly in the host process (as demonstrated in the git engine below), executing system commands with zero network overhead.
  • Client-Side Runtimes: In mobile or web applications, one option is calling Gemini through client SDKs like Firebase AI Logic, allowing the client application to execute local on-device tools directly in-process.
  • Stateless Serverless Services: When tools require private credentials, heavy dependencies, or privileged infrastructure, teams often package the exoskeleton as stateless containerized microservices on platforms like Google Cloud Run. When exposed to client applications, this boundary can be secured with cryptographic attestation like Firebase App Check to prevent unauthorized invocations.
04

The skill distillation flywheel

How do you transition from written skills to deterministic code without losing developer velocity? We employ a three-stage distillation flywheel:

This pattern builds on the code-as-action model pioneered by Voyager (Wang et al., 2023), which demonstrated that autonomous agents scale by synthesizing executable programs into a permanent skill library rather than accumulating prompt instructions:

  1. Stage 1 (Frontier Exploration): Use a frontier model with extended thinking to explore an ambiguous problem space, interact with APIs, and discover edge cases.
  2. Stage 2 (Code Distillation): Once the workflow stabilizes, instruct the frontier model to synthesize the multi-turn interaction into a typed script with strict input and output schemas.
  3. Stage 3 (Light Runtime Deployment): Expose the distilled script as a single tool call. Fast serverless or on-device models invoke the tool with minimal token overhead and zero execution drift.
PART 03

Implementation and benchmarks

Interactive Lab · skill-distillSkill Distillation Simulator

Compare prompt-heavy skills against distilled code-first tools across token cost, latency, and model tiers.

Zero dependencies · runs 100% in your browser · nothing leaves this page
05

TypeScript distilled tool contract

The following implementation demonstrates a distilled tool harness. Instead of injecting a 500-line markdown guide on git branching and commit hygiene, the agent invokes a typed function that enforces invariants deterministically:

This pattern mirrors the architectural direction of the Model Context Protocol (MCP), which standardizes how agents discover and execute typed, deterministic tool contracts rather than parsing natural language instructions in system prompts.

distilledToolHarness.ts
TypeScript
import { z } from 'zod';
import { execFileSync } from 'node:child_process';

// 1. Strict input schema replaces 40 lines of prompt formatting rules
export const CommitActionSchema = z.object({
  branch: z.string().regex(/^[A-Za-z0-9_/-]+$/, 'Invalid branch name format').refine(b => !b.startsWith('-'), 'Branch name cannot start with a dash'),
  message: z.string().min(10).max(72),
  files: z.array(z.string().refine(f => !f.startsWith('/') && !f.includes('..'), 'Must be relative path inside repository')).nonempty(),
  signoff: z.boolean().default(true),
});

export type CommitAction = z.infer<typeof CommitActionSchema>;

// 2. Deterministic execution engine replaces multi-turn prompt retries
export class DistilledGitEngine {
  public static execute(action: CommitAction): { success: boolean; hash?: string; error?: string } {
    try {
      // Validate schema contracts before touching disk
      const validated = CommitActionSchema.parse(action);

      // Safe branch checkout without destructive overwrite
      try {
        execFileSync('git', ['checkout', validated.branch]);
      } catch {
        execFileSync('git', ['checkout', '-b', validated.branch]);
      }

      // Prevent CLI argument injection via '--' delimiter
      execFileSync('git', ['add', '--', ...validated.files]);

      const args = ['commit', '-m', validated.message];
      if (validated.signoff) args.push('--signoff');
      execFileSync('git', args, { encoding: 'utf8' });

      // Retrieve commit hash deterministically rather than parsing stdout
      const hash = execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();

      return {
        success: true,
        hash,
      };
    } catch (err: any) {
      // Return structured, actionable error instead of raw stack trace
      return {
        success: false,
        error: err.message || 'Execution failed',
      };
    }
  }
}
06

Architectural trade-off matrix

When evaluating whether a capability belongs in a written skill or a distilled tool, evaluate the trade-offs across these dimensions:

DimensionPrompt-Heavy Written SkillDistilled Code-First ToolHybrid Distillation Pattern
Context Overhead2,000 to 5,000 tokens per turn50 to 100 tokens (schema only)50 to 100 tokens (schema only)
Execution Latency800 to 3,000 ms per turn2 to 20 ms2 to 20 ms (deterministic code)
Model Tier SupportFrontier reasoning models onlyAll tiers (On-Device, Fast Serverless, Frontier)Frontier for authoring, Fast Serverless for runtime
ReliabilityProbabilistic (varies by task and prompt length)Deterministic (100%)100% verified via unit tests
Authoring VelocityFast initial draftRequires manual engineeringFast (frontier model synthesizes code)

Primary research and documentation