Evidence note: Metrics in this note refer to repository tests or controlled scenarios unless a live production source is explicitly linked.

In multi-agent systems and continuous code-generation pipelines, repetitive API calls to frontier foundation models (OpenAI, Anthropic, Google) create massive compounding financial waste. When an autonomous coding agent executes a 30-turn refactoring loop, it sends nearly identical system instructions, schema definitions, and repository context trees on every single turn.

To solve this, I designed and open-sourced TokensCache: a multi-tier semantic caching layer and token optimization proxy for AI agents, featuring exact hash matching, vector similarity caching, budget limits, and a native Model Context Protocol (MCP) server.

Evidence scope: In controlled repository mock coding-agent A/B scenarios, TokensCache reduced billed token usage by 35.5% while preserving identical output fidelity. This is a scoped engineering benchmark, not a production customer-savings claim.

1. The Multi-Tier Caching Architecture

TokensCache splits incoming agent prompts into a hierarchical two-tier resolution cascade:

  • Tier 1: Exact Hash Matching (SHA-256): For deterministic prompts, tool schemas, and identical context windows. Resolution time is < 2ms with 100% precision.
  • Tier 2: Semantic Cosine Distance (pgvector / HNSW): For semantically equivalent requests with minor phrasing variations. Prompts are embedded using fast quantized vector encoders; queries with a cosine similarity ≥ 0.96 return cached completions instantly.
Metric Direct LLM API With TokensCache Improvement
Billed token usage 100% baseline 64.5% of baseline 35.5% reduction
Output fidelity Reference output Identical output No observed change
Verification Controlled mock scenario 92/92 repository tests Reproducible in the repository

2. Representative Implementation Sketch

The simplified example below explains the resolution strategy. The linked repository is the source of truth for the current production-quality implementation and tests.

// lib/cache/SemanticCacheEngine.ts
import { createHash } from 'crypto';

export interface CacheEntry {
  hash: string;
  embedding: number[];
  prompt: string;
  response: string;
  tokenCount: number;
  timestamp: number;
}

export class SemanticCacheEngine {
  private exactCache = new Map<string, CacheEntry>();
  private vectorEntries: CacheEntry[] = [];
  private readonly threshold: number;

  constructor(threshold = 0.96) {
    this.threshold = threshold;
  }

  public getExact(prompt: string): CacheEntry | null {
    const hash = createHash('sha256').update(prompt.trim()).digest('hex');
    return this.exactCache.get(hash) || null;
  }

  public getSemantic(queryEmbedding: number[]): CacheEntry | null {
    let bestMatch: CacheEntry | null = null;
    let highestSim = -1;

    for (const entry of this.vectorEntries) {
      const sim = this.cosineSimilarity(queryEmbedding, entry.embedding);
      if (sim > highestSim && sim >= this.threshold) {
        highestSim = sim;
        bestMatch = entry;
      }
    }
    return bestMatch;
  }

  private cosineSimilarity(a: number[], b: number[]): number {
    let dot = 0, normA = 0, normB = 0;
    for (let i = 0; i < a.length; i++) {
      dot += a[i] * b[i];
      normA += a[i] * a[i];
      normB += b[i] * b[i];
    }
    return dot / (Math.sqrt(normA) * Math.sqrt(normB));
  }
}

3. Hard Financial Guardrails & Circuit Breakers

Unbounded recursive agent loops can create unexpected spend when an agent gets stuck in a retry cycle. TokensCache introduces explicit budget and rate controls:

  • Per-Session Spend Caps: Configurable hard limits that trip a BudgetExceededCircuitBreaker.
  • Sliding Window Rate Limiter: Throttles burst tool execution to prevent sudden API billing spikes.
  • Model Fallback Hierarchy: Automatically degrades non-critical summarization sub-tasks from GPT-5/Opus to high-efficiency flash models.

Sources, Code & Further Reading

  • Open Source Codebase: github.com/kais-aljammal/tokenscache — Complete TypeScript semantic caching & MCP server engine.
  • Prompt Caching Research: Anthropic Research. (2024). Prompt Caching with Claude: Reducing Latency and Costs for Long Prompts. Anthropic Docs.
  • Model Context Protocol Specification: Anthropic & Community. (2024). Model Context Protocol (MCP). modelcontextprotocol.io.
  • HNSW Vector Indexing: Malkov, Y. A., & Yashunin, D. A. (2018). Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs. IEEE TPAMI. arXiv:1603.09320.