Sitemap
Lovlesh Pokra

Lovlesh Pokra

Experienced Software Professional | Writes about JavaScript, Technology, Web Performance, Web Security, React.js, Node.js, etc.

Reduce AI skill execution cost

7 min readOct 30, 2026

--

Press enter or click to view image in full size
AI Agents Working

When teams transition from basic prompt-based instructions to autonomous AI coding agents (such as SWE-bench pipelines, Cursor, Claude, or custom internal orchestration engines), they face an aggressive economic barrier: unbounded token consumption and high cost.

Naive agent implementations that dump full repository file trees into context, maintain unpruned multi-turn chat logs, and run unbounded trial-and-error loops rapidly become unsustainable. Beyond the direct financial impact, bloated context windows degrade LLM reasoning through needle-in-a-haystack attention dilution, inflate Time to First Token (TTFT) latency, and trigger compounding hallucination loops.

I’ll give you a comprehensive guide for engineering low-cost, high-precision AI coding agents:

Task Graphs, Scoped Access, and Sub-Agent Context Isolation

Monolithic prompts like “Fix the session management bug across the codebase” force models to ingest, parse, and retain thousands of lines of unrelated code across every single turn.

High-performance agent architectures eliminate this waste by decoupling planning from execution using Directed Acyclic Task Graphs (DATGs) paired with strict read/write boundaries.


┌────────────────────────┐
│ Task Graph Orchestrator│
└───────────┬────────────┘
│
┌───────────────────────────┼───────────────────────────┐
▼ ▼ ▼
┌───────────────────┐ ┌───────────────────┐ ┌───────────────────┐
│ Discovery Node │ │ Analysis Node │ │ Execution Node │
│ (Read-Only AST) │ │ (Symbol Index) │ │ (Write-Gated) │
└─────────┬─────────┘ └─────────┬─────────┘ └─────────┬─────────┘
│ │ │
└───────────────────────┼───────────────────────┘
▼
┌───────────────────────┐
│ Validation Node │
│ (Deterministic CLI) │
└───────────────────────┘

Decomposition

  1. Explicit Operational Nodes: Divide every engineering objective into isolated execution phases:
  • Discovery Phase: Scans types, signatures, and file trees (Read-Only access).
  • Planning Phase: Generates a minimal execution plan (Read-Only access).
  • Patch Phase: Applies localised changes to targeted files (Write-Gated access).
  • Validation Phase: Runs targeted test suites (Execution-Only access).

2. Scoped Read/Write Permissions: Enforce file-system access bounds at the tool tier. An execution agent tasked with modifying src/auth/jwt.ts must be strictly isolated from src/ui/components/Navbar.tsx. Stripping unreferenced codebase paths from the execution agent's tool environment drops prompt payload sizes by up to 90%.

Get Lovlesh Pokra’s stories in your inbox

Join Medium for free to get updates from this writer.

3. Sub-Agent Context Isolation: Rather than passing the full conversational state of the planner to the implementer, the orchestrator passes only the specific JSON specification required for that node.

Context Engineering: Maximising Signal-to-Noise Ratio

Context engineering is the deliberate practice of maximising the ratio of high-value signal tokens to low-value noise tokens before sending a payload to the model.

Raw Source File (600 Lines / ~4,500 Tokens)
└─► AST Parsing (Tree-Sitter)
└─► Pruned Symbol Outline (Signature Only / ~150 Tokens) [96.6% Token Reduction]

High-Impact Pruning Strategies

  • AST-Powered Symbol Slicing: Replace full source file dumps with lightweight symbol outlines using language parsers like Tree-Sitter or Babel. Inject only interface signatures, function headers, and export declarations:
// Raw File (600 lines) reduced to minimal AST interface outline (15 tokens)
export interface UserService {
getUserById(id: string): Promise<User>;
updatePreferences(id: string, prefs: UserPreferences): Promise<void>;
}
  • Hybrid Search (Lexical + Dense Vector Retrieval): Avoid inserting full documentation trees or system files into context windows. Deploy a hybrid retrieval pipeline (BM25 lexical search combined with dense vector embeddings) to retrieve only the relevant 15–30 line code blocks.
  • Diff-Only Context Tracking: During sequential code modifications, strip historical file states from the active prompt memory. Retain only the base symbol signatures and the running Git diff output.

Structured Prompts and Schema-Constrained Decoding

Conversational chatter (“Sure! As an AI assistant, I would be happy to help you refactor…”) wastes valuable output tokens. Because model providers charge 3x to 4x more for output tokens than input tokens, eliminating conversational padding directly reduces operating costs.

Enforcing Structural Constraints

Utilise JSON Schema enforcement or Grammar-Constrained Decoding (via native tool-calling APIs or frameworks like Outlines and Instructor) to force machine-readable, concise payloads:

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "CodePatchPayload",
"type": "object",
"properties": {
"target_filepath": { "type": "string" },
"original_snippet": { "type": "string" },
"replacement_snippet": { "type": "string" }
},
"required": ["target_filepath", "original_snippet", "replacement_snippet"]
}

By constraining the output grammar at the token generation layer, you eliminate token expenditure on introductory framing, markdown wrappers, or unsolicited conversational prose.

Controlled Workflows, State Machines, and Loop Guardrails

Unbound agent loops (e.g., “keep modifying code until the unit test passes”) are the leading driver of unexpected financial runaway. Cost-efficient orchestration requires deterministic finite-state machines (FSMs) with strict execution guardrails.

       ┌────────────────────────┐
│ Executed Code Patch │
└───────────┬────────────┘
│
▼
┌────────────────────────┐
│ Static Linter / Compiler│
└───────────┬────────────┘
│
┌─────────┴─────────┐
│ Passes Check? │
└────┬─────────┬────┘
YES │ │ NO
┌───────────┘ └──────────┐
▼ ▼
[Complete] [Increment Retry Count]
│
┌─────────┴─────────┐
│ Retry Count > 2? │
└────┬─────────┬────┘
YES │ │ NO
┌───────────┘ └──────────┐
▼ ▼
[Escalate to Human] [Re-invoke LLM with]
[ or Tier 3 Model ] [ Minimal Error Log ]

Loop Optimisation Protocols

  • Deterministic-First Validation: Run local, non-LLM static analysis tools (tsc, ESLint) before passing execution results back to the LLM. Let free CLI utilities uncover missing semicolons and syntax errors instead of spending LLM inference cycles to discover syntax typos.
  • Hard Iteration Caps: Enforce a strict ceiling of 2 to 3 retry attempts per sub-task node.
  • Tiered Model Escalation: If an inexpensive execution agent fails twice on a static linter check, terminate the loop and escalate the failure payload to a higher-tier reasoning model or a human developer.

Specialised Sub-Agents, Delegation, and Skill Modularisation

Generalist developer prompts attempt to instruct a single agent on every framework convention, architectural pattern, and testing rule simultaneously. This results in bloated system prompts that dilute model focus.

The Sub-Agent Delegation Model

Deconstruct monolithic agents into modular sub-agents with specialised, lightweight prompts:

  1. Router / Triage Agent (Fast/Cheap Model): Analyses the user request and routes it to the specific sub-graph node.
  2. Type Authoring Agent: Specialised strictly in generating TypeScript types, Zod schemas, or database migrations.
  3. Patch Execution Agent: Focused exclusively on applying high-precision, minimal string replacements.
  4. Unit Test Specialist: Focused entirely on boundary testing and assertion coverage.

Memory Architecture and Context Window Optimisation

Long-running agent sessions accumulate extensive interaction logs that become exponentially expensive to re-process on every turn.

Memory Architectures

  • Sliding Window with Summary Checkpoints: Compress conversation turns older than $N-2$ into a brief, immutable state summary (e.g., “Step 2 complete: Updated UserReducer interface to include refreshToken”), dropping raw diffs and verbose tool call logs.
  • Externalised State Stores: Maintain an external state object (in Redis, SQLite, or in-memory) to track task metadata, modified file lists, and compiler errors. Inject this minimal state dictionary into the system prompt rather than re-sending raw chat history.
  • Prefix-Aligned Prompt Caching: Order prompts so that static elements (System Rules, API Specs, Base Schemas) sit at the absolute top of the context payload, while dynamic content (User Inputs, Current Diffs) is appended at the very end. This structure leverages provider-level Prompt Caching (e.g., Anthropic Prompt Caching, OpenAI Automatic Caching) to yield 50% to 90% cost savings on prompt input tokens.

Eliminating Hallucinations to Prevent Cost Multipliers

Model hallucinations introduce compounding execution costs. When an agent invents a non-existent API method or external dependency, it triggers failing builds, prolonged diagnostic loops, and wasted refactoring passes.

Hallucination Prevention Vectors

  • Grounding via Symbol Lookups: Require the agent to perform an explicit symbol search or type inspection tool call before generating code that references internal codebase entities.
  • Import Whitelisting: Disallow agents from guessing third-party package names. Force agents to inspect package.json or requirements.txt before importing external modules.
  • Low-Temperature Code Generation: Enforce low sampling temperatures ($0.0–0.2$) on execution nodes to guarantee deterministic, highly grounded code outputs.

Targeted Sub-Prompts and Pre-Flight Skill Verification

Before initiating large code generation passes, agents should execute a low-token pre-flight verification step to confirm they possess the required context.

Pre-Flight Verification Protocol

Inject a micro-checklist instruction before code generation nodes execute:

[SYSTEM INSTRUCTION: PRE-FLIGHT CHECKLIST]
Before generating implementation code, confirm:
1. Do you have the explicit interface/type definition for the target function? (If NO -> Invoke `get_symbol_definition`)
2. Have you verified the active testing framework? (If NO -> Inspect config file)
3. Are there active linting constraints on this file? (If YES -> Retain constraints)

Proceed to code generation ONLY after all pre-flight dependencies are confirmed.Proceed to code generation ONLY after all pre-flight dependencies are confirmed.

Ensuring the agent resolves context gaps before writing substantial code blocks prevents costly retry cycles caused by missing environmental assumptions.

Advanced Systems-Level Optimisation Techniques

Beyond prompt and context design, several systems-level strategies further optimise AI agent execution economics:

Model Cascading and Tiered Routing

Route micro-tasks dynamically based on complexity. Use ultra-low-cost models (e.g., Claude 3.5 Haiku, GPT-4o-mini, or open-weight 8B models) for formatting, AST extraction, and intent classification. Reserve flagship reasoning models (e.g., Claude 3.5 Sonnet, O1/O3 series) exclusively for complex architectural refactoring and multi-file logic changes.

Exact-Match and Semantic Tool Caching

  • Exact-Match Tool Caching: Cache deterministic tool call outputs (e.g., AST parses, schema validations, dependency trees) using SHA-256 hashes of input payloads.
  • Semantic Embedding Caching: Implement a vector similarity cache (e.g., Redis Vector Search) for common technical queries or boilerplate generation. If an incoming sub-prompt exceeds a 0.96 cosine similarity threshold with a prior validated run, return the cached artefact instantly without hitting the LLM API.

The bottom line

AI coding agents are no longer experimental novelties; they are core compute engines. Yet, treating them like a bottomless well of tokens is an expensive architectural failure mode. By replacing monolithic prompts with Directed Acyclic Task Graphs (DATGs), offloading raw file dumps to AST symbol slicing, enforcing constrained JSON decoding, and routing tasks via tiered model cascading, teams routinely slash agent execution costs by 70% to 85% while simultaneously eliminating attention drift and hallucinations. The math is unforgiving: untamed agent loops will bankrupt your engineering margins. Treat every token as a scarce resource, constrain your sub-agents with strict read/write boundaries, and engineer your context pipelines with the same rigour you apply to database queries.

Happy reading and learning… Please don’t forget to clap.

Learn more about me @ LinkedIn, Portfolio, GitHub, HackerRank or book virtual coffee with Lovlesh

--

--

Lovlesh Pokra

Written by Lovlesh Pokra

Experienced Software Professional | Writes about JavaScript, Technology, Web Performance, Web Security, React.js, Node.js, etc.