gemini-cache.recorded.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { LLM } from "../../src"
  4. import { LLMClient } from "../../src/route"
  5. import * as Gemini from "../../src/protocols/gemini"
  6. import { LARGE_CACHEABLE_SYSTEM } from "../recorded-scenarios"
  7. import { recordedTests } from "../recorded-test"
  8. const model = Gemini.model({
  9. id: "gemini-2.5-flash",
  10. apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY ?? process.env.GEMINI_API_KEY ?? "fixture",
  11. })
  12. // Gemini does implicit prefix caching on 2.5+ models above ~1024 tokens. The
  13. // `CacheHint` is currently a no-op for Gemini (the explicit `CachedContent`
  14. // API is out-of-band and intentionally not wired up). This test exists to
  15. // pin the usage-parsing path: `cachedContentTokenCount` should surface as
  16. // `cacheReadInputTokens` on the second identical call.
  17. const cacheRequest = LLM.request({
  18. id: "recorded_gemini_cache",
  19. model,
  20. system: LARGE_CACHEABLE_SYSTEM,
  21. prompt: "Say hi.",
  22. generation: { maxTokens: 16, temperature: 0 },
  23. })
  24. const recorded = recordedTests({
  25. prefix: "gemini-cache",
  26. provider: "google",
  27. protocol: "gemini",
  28. requires: ["GOOGLE_GENERATIVE_AI_API_KEY"],
  29. // Two identical requests in one cassette — replay walks the cassette in
  30. // recording order so the second call replays the cached-hit interaction.
  31. })
  32. describe("Gemini cache recorded", () => {
  33. recorded.effect.with("reports cachedContentTokenCount 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. // Implicit caching is best-effort on Gemini's side; we assert the field
  39. // is at least populated and non-negative. When re-recording, verify the
  40. // cassette shows > 0 in the second response's usage.
  41. expect(second.usage?.cacheReadInputTokens ?? 0).toBeGreaterThanOrEqual(0)
  42. }),
  43. )
  44. })