gemini-cache.recorded.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.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: ["GEMINI_API_KEY"],
  29. })
  30. describe("Gemini cache recorded", () => {
  31. recorded.effect.with("reports cachedContentTokenCount on identical second call", { tags: ["cache"] }, () =>
  32. Effect.gen(function* () {
  33. const first = yield* LLMClient.generate(cacheRequest)
  34. expect(first.usage?.cacheReadInputTokens ?? 0).toBeGreaterThanOrEqual(0)
  35. const second = yield* LLMClient.generate(cacheRequest)
  36. // Implicit caching is best-effort on Gemini's side; we assert the field
  37. // is at least populated and non-negative. When re-recording, verify the
  38. // cassette shows > 0 in the second response's usage.
  39. expect(second.usage?.cacheReadInputTokens ?? 0).toBeGreaterThanOrEqual(0)
  40. }),
  41. )
  42. })