🤖 AI 资讯

· · ↗
← 返回列表

Your Vercel AI SDK agent already knows the answer. Stop making it re-derive it.

DEV.to2026-11-08 05:00:49大模型,AI应用,Agent智能体,扩散模型,强化学习,模型评测,提示工程,端侧AI,招聘HR,榜单评测,开发者生态原文 ↗

Your Vercel AI SDK agent already knows the answer. Stop making it re-derive it.

Your Vercel AI SDK agent just spent 3.9 seconds and a full tool loop answering a goal it answered yesterday. Different user, slightly different wording, same intent — and the agent re-derived the whole plan from scratch: same tool choices, same order, same result. The model call isn't the expensive part. The re-derivation is.

This is the loop tax, and it hits every framework the same way. Vercel AI SDK's generateText with tools, Mastra's agent runs, a hand-rolled loop — they all re-run the same goals all day, because nothing in the stack notices that the trajectory repeated. Prompt caching at the model layer saves you per-call. It doesn't notice that the plan repeated.

Pomerene is a deterministic trajectory cache for agentic workflows, and the integration model is deliberately boring: it wraps your agent function, not your model client. Your prompts are never touched, rewritten, or intercepted. That matters for Vercel AI SDK and Mastra, because the cache doesn't need to know anything about your framework's internals — only that it can record a run and replay the recorded steps when a matching goal arrives.

The integration: one wrap, or two HTTP calls

There are two integration shapes, and both are framework-proof — the same code works whether your agent runs on Vercel AI SDK, Mastra, LangGraph, or a hand-rolled loop, because the unit being cached is the trajectory, not the framework object.

One wrap:

import { withCache } from "@pomerene/core"; // from the self-hosted Starter Kit

const cachedAgent = withCache(myAgent, {
  // policy: what may be fuzzy-matched, what may never be
  writeSteps: "never-fuzzy",   // steps that mutate state are never fuzzy-matched
  drift: "handoff",            // partial drift hands control back to the agent
});

const result = await cachedAgent.run("refund the extra charge on my last order");

Or two HTTP calls, in any language:

POST /v1/lookup { goal, context }   # before your agent runs
POST /v1/store   { goal, trajectory } # after it finishes

L1 returns the cached outcome. L2 resumes your agent mid-trajectory. A miss runs live and gets stored. That's the whole integration — and it works the same way in a Vercel AI SDK tool loop as in a Mastra agent, because in both cases the agent is a function: goal in, result out, plus a recorded trajectory in between.

What a hit actually looks like

Three outcomes, all deterministic — hashing plus vector geometry computed in TypeScript, no LLM call in the match decision, so the same input makes the same cache decision every time:

  1. L1 — full replay. The incoming goal matches a cached trajectory end to end. The stored plan replays: a 14-step audit trajectory replays in 47ms versus 3.9s live, with zero LLM invocation on the hit path.
  2. L2 — partial replay. The cached plan matches up to a point, then diverges. Pomerene replays the matching prefix and hands control back to your agent at the exact step where the plan stops matching. The agent only does the work that's actually new — the tool loop resumes mid-trajectory instead of restarting.
  3. Miss. No usable match — the request runs through your agent normally, and the completed trajectory is recorded for future hits.

On τ-bench — the airline customer-service benchmark, dense with the rephrased-same-goal traffic that support agents actually see — the engine served 86% of paraphrased requests from cache, with 78% of those judged correct. Read both numbers honestly: 86% is why repetitive agent workloads are worth caching, and 78% is why the write-safety rules below are the product, not a footnote.

The part that has to be true before you cache a write

A Vercel AI SDK agent with tools can charge a card, send a message, mutate a database. A Mastra agent the same. So the write rule is the whole product: steps that write are never fuzzy-matched against a new request. A cached write step replays only on an exact goal-plus-context repeat; anything ambiguous becomes a miss or an L2 handoff. Partial drift produces a handoff to your agent, never a subtly wrong execution. On an adversarial eval of polarity-flipped pairs (requests that are semantically close but must not be confused, like activate vs. deactivate at 0.853 cosine similarity), the gated engine produced 0 false positives across 18 pairs.

One honest note, because the gate's story changed after the first post about it: the write-safety gate as originally published had three holes, found via outside critique and fixed in core — merged, shipping in the next core release. An abstract-goal match is no longer treated as an exact repeat (write trajectories are refused there, and context is diffed); a trajectory with no taint records plus a context change is now a miss; and a hit never serves past a declared external_write — control hands back to the agent at that step. Still open, not shipped: entity agreement on read matches. The numbers above describe the gate as designed; the fixes are what keep the design honest, and the open item is the next gate to build.

Measure first, then wrap

If your agent's traffic is mostly unique, one-off tasks, this buys you less — and the kit will tell you that before you commit. The built-in diagnostics (/v1/diagnostics) show hit rates, the L1/L2/miss breakdown, latency percentiles, and dollars and tokens saved for your real traffic. If your traces show the same goals arriving in different wording, you're paying the model to rediscover plans it already derived. A wrapper turns that rediscovery into a replay — in your Vercel AI SDK loop, your Mastra agent, or wherever the agent lives.

Pomerene ships as a self-hosted Starter Kit — Indie $99 / Pro $249, one-time, source included — so the cache runs in your infrastructure, next to your agents. Quickstart is about 5 minutes (Node 20+, Docker or Postgres 15+). Docs and details at pomerene.io/starter-kit.