Skip to content

Commit d394ec8

Browse files
authored
Allow caching of tool calls (commontoolsinc#2012)
1 parent 83befa0 commit d394ec8

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

packages/toolshed/routes/ai/llm/llm.handlers.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,21 @@ export const generateText: AppRouteHandler<GenerateTextRoute> = async (c) => {
126126
payload.metadata.user = user;
127127
}
128128

129-
// Disable caching for requests with tools to prevent incomplete cached responses
130-
// Tool calls are executed client-side, so server cache doesn't include tool results
131-
const hasTools = payload.tools && Object.keys(payload.tools).length > 0;
132-
const shouldCache = payload.cache && !hasTools;
133-
134-
if (payload.cache && hasTools) {
135-
console.log(
136-
"Caching disabled for request with tools to prevent incomplete cached responses",
137-
);
138-
}
129+
// Enable caching for all requests including those with tools.
130+
// With the sequential request architecture, each request includes complete context
131+
// (including tool results from previous rounds), making each response cacheable.
132+
//
133+
// Cache key naturally includes:
134+
// - Original user message
135+
// - Tool definitions
136+
// - Tool results (in messages array for subsequent rounds)
137+
// - Full conversation history
138+
//
139+
// Note: Non-deterministic tools will be cached. If tool results change for the
140+
// same inputs, the different results will produce different cache keys, resulting
141+
// in a cache miss (correct behavior). For intentional cache invalidation, use
142+
// the cache eviction utilities or modify the request to produce a different hash.
143+
const shouldCache = payload.cache === true;
139144

140145
let cacheKey: string | undefined;
141146
if (shouldCache) {

0 commit comments

Comments
 (0)