Skip to content

Commit 62a7ac2

Browse files
t3dotggclaude
andcommitted
fix(server): delegated Claude children write a 5m prompt cache instead of 1h
Closes #22749 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 96f09c6 commit 62a7ac2

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,30 @@ describe("ClaudeAdapterV2 runtime query policy", () => {
249249
assert.isUndefined(options.extraArgs?.["dangerously-skip-permissions"]);
250250
});
251251

252+
it("gives delegated children a 5m prompt cache unless the instance sets a TTL", () => {
253+
const lineage = (relationshipToParent: "fork" | "subagent" | null) => ({
254+
lineage: {
255+
parentThreadId: null,
256+
relationshipToParent,
257+
rootThreadId: ThreadId.make("thread-root"),
258+
},
259+
});
260+
const env = { PATH: "/bin" };
261+
assert.equal(
262+
ClaudeAdapterV2.claudeQueryEnvironment(env, lineage("subagent")).CLAUDE_CODE_PROMPT_CACHE_TTL,
263+
"5m",
264+
);
265+
assert.strictEqual(ClaudeAdapterV2.claudeQueryEnvironment(env, lineage(null)), env);
266+
assert.strictEqual(ClaudeAdapterV2.claudeQueryEnvironment(env, lineage("fork")), env);
267+
assert.equal(
268+
ClaudeAdapterV2.claudeQueryEnvironment(
269+
{ ...env, CLAUDE_CODE_PROMPT_CACHE_TTL: "1h" },
270+
lineage("subagent"),
271+
).CLAUDE_CODE_PROMPT_CACHE_TTL,
272+
"1h",
273+
);
274+
});
275+
252276
it("projects AskUserQuestion input with question text as the answer key", () => {
253277
const preview = "\n **Proposed change**\n\n```ts\n\tconst enabled = true;\n```\n ";
254278
assert.deepEqual(

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,22 @@ function claudeLiveSelectionFlagSettings(
12441244
};
12451245
}
12461246

1247+
// Claude Code gives SDK sessions a 1h prompt cache on subscriptions, but a delegated child runs a
1248+
// tight tool loop like a native subagent, which gets 5m. A 1h write costs 2x base input and a 5m
1249+
// write 1.25x, so children use 5m unless the provider instance sets its own TTL.
1250+
export function claudeQueryEnvironment(
1251+
environment: NodeJS.ProcessEnv,
1252+
appThread: Pick<OrchestrationV2AppThread, "lineage">,
1253+
): NodeJS.ProcessEnv {
1254+
if (
1255+
appThread.lineage.relationshipToParent !== "subagent" ||
1256+
environment.CLAUDE_CODE_PROMPT_CACHE_TTL !== undefined
1257+
) {
1258+
return environment;
1259+
}
1260+
return { ...environment, CLAUDE_CODE_PROMPT_CACHE_TTL: "5m" };
1261+
}
1262+
12471263
export function makeClaudeQueryOptions(input: {
12481264
readonly modelSelection: ModelSelection;
12491265
readonly nativeThreadId: string;
@@ -9154,7 +9170,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
91549170
cwd: turnInput.runtimePolicy.cwd,
91559171
attachmentsDir,
91569172
settings: adapterOptions.settings,
9157-
environment: adapterOptions.environment,
9173+
environment: claudeQueryEnvironment(adapterOptions.environment, turnInput.appThread),
91589174
tools: queryPolicy.tools ?? CLAUDE_CODE_PRESET_TOOLS,
91599175
...(mcpOverrides.allowedTools === undefined
91609176
? {}

0 commit comments

Comments
 (0)