Skip to content

U1: Response cache aggressive mode#9

Open
jaturapornchai wants to merge 1 commit into
mainfrom
worktree-agent-a645af7b
Open

U1: Response cache aggressive mode#9
jaturapornchai wants to merge 1 commit into
mainfrom
worktree-agent-a645af7b

Conversation

@jaturapornchai

Copy link
Copy Markdown
Owner

Summary

  • Enable response cache by default (RESPONSE_CACHE_ENABLED=0 to disable)
  • Bump TTL 300s -> 3600s (1 hour)
  • Drop temp > 0.3 and tools.length > 0 skip conditions; keep stream === true skip
  • Include tool_choice in cache key hash alongside existing tools/messages/model/temperature
  • Rewrite header comment to describe current aggressive behavior

Cross-response bleeding risk is mitigated because the sha256 key includes the full messages array and tool definitions — two requests only collide if every field is byte-identical.

Test plan

  • rtk npx next build -> Errors: 0 | Warnings: 0
  • Pre-existing test failures in scanner.test.ts are unrelated to this change

Copilot AI review requested due to automatic review settings April 9, 2026 21:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Enables “aggressive mode” response caching for non-stream chat completions by default, increasing cache hit rate and reducing repeat LLM work at the gateway layer.

Changes:

  • Enable response caching by default via RESPONSE_CACHE_ENABLED !== "0".
  • Increase Redis TTL from 300s to 3600s and simplify skip logic to only exclude streaming.
  • Expand cache key inputs to include tool_choice in addition to model/messages/temperature/tools.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib/response-cache.ts
Comment on lines 19 to 25
const messages = body.messages;
const model = body.model ?? "auto";
const temperature = body.temperature ?? 0;
const tools = body.tools ?? null;
const payload = JSON.stringify({ model, messages, temperature, tools });
const tool_choice = body.tool_choice ?? null;
const payload = JSON.stringify({ model, messages, temperature, tools, tool_choice });
const hash = createHash("sha256").update(payload).digest("hex").slice(0, 32);

Copilot AI Apr 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache key only hashes {model, messages, temperature, tools, tool_choice}. However the gateway accepts and forwards other request parameters that materially change the output (e.g., response_format/json_schema, max_tokens/max_completion_tokens, top_p, stop, presence_penalty, frequency_penalty, logit_bias, n, seed, etc.). With aggressive caching enabled by default, two requests that differ in any of those fields can incorrectly collide and return an incompatible cached response. Consider hashing a normalized copy of the full request body (at least all output-affecting fields), excluding only non-semantic fields like stream (and possibly stream_options/store after confirming they don't affect provider output).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants