Prompt caching
Pattern
BEG·Jun 26

OpenAI Prompt Caching (AI Gateway)

Lightweight ToolLoopAgent pattern for OpenAI prompt caching through Vercel AI Gateway: stable system instructions, promptCacheKey, and per-turn cachedPromptTokens metrics.

View Full Pattern on Desktop
OpenAI Prompt Caching (AI Gateway) preview

Problems Solved

  • OpenAI prompt caching with promptCacheKey via AI Gateway
  • Stable system prefix for cache hits across agent turns
  • Inspect cachedPromptTokens from provider metadata

Use Cases

  • Reduce cost on repeated agent system prompts
  • Teach promptCacheKey grouping with ToolLoopAgent
  • Compare cache metrics across multi-turn sessions

AI SDK APIs

ToolLoopAgentgatewayproviderOptionsstepCountIs

Tags

aiagentsprompt-cacheopenaiai-gatewayprovider-optionstool-loop-agent

Requirements

Node.js
24+
Memory
256MB
Other
  • Vercel AI Gateway API key

Environment Variables

External Services

openai

Ready to use?

View the full interactive pattern with live preview and copy the source code.

View Pattern on Desktop
End of Pattern