Repository navigation
Open
Labels
Description
Summary
AI Gateway response caching is different from provider prompt/prefix caching. Cloudflare's current AI Gateway caching docs make the distinction concrete:
- Gateway cache only applies to identical requests unless callers provide
cf-aig-cache-key. - Per-request controls are
cf-aig-cache-ttl,cf-aig-skip-cache, andcf-aig-cache-key. - Responses expose cache status with
cf-aig-cache-statusasHITorMISS.
llm-providers already has pieces of this surface (GatewayMetadata.cacheKey, GatewayMetadata.cacheTtl, ResponseCacheAdapter, and CacheHints.strategy: 'response' | 'both'), but downstream consumers still need to know too much about which fields affect Cloudflare AI Gateway versus local response-cache adapters.
Docs:
Current code surface
BaseProvider.getAIGatewayHeaders()forwardscf-aig-cache-keyandcf-aig-cache-ttlwhen the provider base URL is AI Gateway.GatewayMetadatadoes not exposeskipCache.CacheHints.strategy: 'response'is documented, but does not itself create Gateway response-cache headers or an explicit local adapter policy.- Provider response metadata does not normalize
cf-aig-cache-statuswhen Gateway returns it. - The factory-level
ResponseCacheAdapterkey is internal and separate from AI Gateway's cache key behavior.
Proposed work
- Add explicit
GatewayMetadata.skipCache?: booleanmapped tocf-aig-skip-cache: truefor HTTP-provider Gateway calls. - Add response metadata normalization for
cf-aig-cache-statuswhere provider adapters can access response headers. - Document how
CacheHints.strategyrelates toGatewayMetadataandResponseCacheAdapter:provider-prefix: provider/native prompt cache only.response: response-cache policy only, but caller must provide Gateway metadata or a response cache adapter.both: both layers, still separately observable.
- Consider a small helper for deterministic cache key generation that consumers can call before dispatch, so gateway/custom cache keys are stable without copying factory internals.
- Add tests that prove old Cloudflare headers still work and the new skip-cache/status surfaces are backward-compatible.
Acceptance criteria
GatewayMetadata.skipCacheis exported, documented, and mapped to the current Cloudflare header name.- Provider responses routed through AI Gateway can expose normalized cache status, at least as
response.metadata.aiGatewayCacheStatus. - README shows a minimal AI Gateway response-cache example with
cacheKey,cacheTtl, andskipCache. - Tests cover new headers and avoid sending Cloudflare-specific headers to non-Gateway base URLs.
Notes
Do not make response caching default for arbitrary chat/agent turns. Gateway response caching is exact-request caching and should remain an explicit policy choice.
Activity
stackbilt-admin commentedon Jun 9, 2026
AEGIS has started using the newer Cloudflare Gateway/binding contract from
@stackbilt/llm-providers@^1.14.1.Additional acceptance criteria from the AEGIS integration:
x-session-affinity/ prefix caching separate from AI Gateway response caching in examples and metadata naming.AEGIS direct binding calls now attach route metadata when
AI_GATEWAY_IDis set; provider-backed calls passgatewayMetadata.customMetadata.app = 'aegis'and route class where available.stackbilt-admin commentedon Jun 9, 2026
Update 2026-06-09: llm-gateway now forwards Workers AI calls through AI Gateway when AI_GATEWAY_ID is set and passes cache/observability metadata through the binding shim. Gateway commit: Stackbilt-dev/llm-gateway@b2062c1.\n\nThe gateway now sends cf-aig-gateway-id, cf-aig-cache-key/cache-ttl, cf-aig-metadata, and x-session-affinity for provider-prefix cache affinity. Added a focused gateway unit test for the AI Gateway chat-completions path and header forwarding.\n\nRelated provider catalog work is pushed as 273b314 for Kimi K2.6, GLM-4.7-Flash, and deepseek/deepseek-v4-pro; npm publish is blocked by registry auth (npm whoami 401).
stackbilt-admin commentedon Oct 13, 2026
Local implementation checkpoint — 2026-10-12
This work is uncommitted/unpushed on
main, based on8a9eda6c058d63f0ec08332f6a408aaf409232cf, and is not merged or released. Keep this issue open pending review/integration. The local handoff isdocs/maintenance/issue-sweep.md; it will travel with the code.The #108 factory-cache implementation and docs now make local response-cache policy explicit:
off/provider-prefixbypass local response caching;response/bothuse the caller's configuredResponseCacheAdapter.This sweep did not implement a new HTTP AI Gateway skip/status surface. Existing
GatewayMetadata.skipCacheand other Gateway code predate this patch and need an independent acceptance audit against this issue before closure. No public deterministic Gateway-key helper was added; the factory key helper is internal.Validation for the combined local sweep: 605 tests pass across 27 files, TypeScript typecheck passes, Worker-targeted bundle/import smoke passes. Packed entrypoint exports/client methods and all 33 JavaScript sourcemaps were checked on Windows with Node 24.19.0/npm 11.17.0. No live/billable providers or workerd execution; this does not prove minimum Node-version compatibility.