openai-responses-cache.recorded.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { LLM } from "../../src/index.js"
  4. import { LLMClient } from "../../src/route.js"
  5. import * as OpenAI from "../../src/providers/openai.js"
  6. import { LARGE_CACHEABLE_SYSTEM } from "../recorded-scenarios.js"
  7. import { recordedTests } from "../recorded-test.js"
  8. const model = OpenAI.configure({
  9. apiKey: process.env.OPENAI_API_KEY ?? "fixture",
  10. }).responses("gpt-4.1-mini")
  11. // OpenAI caches prefixes automatically once they cross the 1024-token threshold;
  12. // `CacheHint` is a no-op for the wire body. The stable signal is the
  13. // `prompt_cache_key` routing hint, which keeps repeated calls on the same shard
  14. // so cache hits are observable.
  15. const cacheRequest = LLM.request({
  16. id: "recorded_openai_responses_cache",
  17. model,
  18. system: LARGE_CACHEABLE_SYSTEM,
  19. prompt: "Say hi.",
  20. generation: { maxTokens: 16, temperature: 0 },
  21. promptCacheKey: "recorded-cache-test",
  22. })
  23. const recorded = recordedTests({
  24. prefix: "openai-responses-cache",
  25. provider: "openai",
  26. protocol: "openai-responses",
  27. requires: ["OPENAI_API_KEY"],
  28. // Two identical requests in one cassette — replay walks the cassette in
  29. // recording order so the second call replays the cached-hit interaction,
  30. // not the cold-miss one.
  31. })
  32. describe("OpenAI Responses cache recorded", () => {
  33. recorded.effect.with("reports cached_tokens on identical second call", { tags: ["cache"] }, () =>
  34. Effect.gen(function* () {
  35. const first = yield* LLMClient.generate(cacheRequest)
  36. expect(first.usage?.cacheReadInputTokens ?? 0).toBeGreaterThanOrEqual(0)
  37. const second = yield* LLMClient.generate(cacheRequest)
  38. expect(second.usage?.cacheReadInputTokens ?? 0).toBeGreaterThan(0)
  39. }),
  40. )
  41. })