Prompt caching

Pattern
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.
BeginnerJun 2026View the full pattern on desktop


Problems solved
- Enable OpenAI prompt caching with promptCacheKey via AI Gateway
- Reuse a stable system prefix across ToolLoopAgent turns
- Inspect cachedPromptTokens from provider metadata
Use cases
- Lowering cost on repeated agent system prompts
- Teaching promptCacheKey grouping with ToolLoopAgent
- Comparing 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
AI_GATEWAY_API_KEYGet key
External services
openai
Ready to use?
View the full interactive pattern with live preview and copy the source code.
View the pattern on desktop






