Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

enable prefix cache for anthropic by default (#207)

Heer Ambavi08b5b50b8c19
diff --git a/packages/harness/src/core/llm/VercelAILLM.ts b/packages/harness/src/core/llm/VercelAILLM.ts
index bd7740d40..3e72db208 100644
--- a/packages/harness/src/core/llm/VercelAILLM.ts
+++ b/packages/harness/src/core/llm/VercelAILLM.ts
@@ -371,19 +371,22 @@ function openaiProviderOptions({
* Forwards but never pins `thinking` and `effort`: both replace the adapter's per-model resolution
* and no single value fits every model. This is the caller's only route to disabling thinking, or to
* the `display: 'summarized'` that makes Claude 5 return reasoning text instead of an empty block.
+ *
+ * Prompt caching defaults on: Anthropic bills a write once, then cheaper reads on stable prefixes
+ * (system / tools / early messages). Callers can override via `cache_control` in the request body.
*/
function anthropicProviderOptions(rawBody: unknown): JSONObject | undefined {
- const cacheControl = readBodyField({ rawBody, key: 'cache_control' });
+ const cacheControl = readBodyField({ rawBody, key: 'cache_control' }) ?? { type: 'ephemeral' };
const disableParallelToolUse = readBodyField({ rawBody, key: 'disable_parallel_tool_use' });
const thinking = readBodyField({ rawBody, key: 'thinking' });
const effort = readBodyField({ rawBody, key: 'effort' });
const opts: JSONObject = {
- ...(cacheControl !== undefined ? { cacheControl } : {}),
+ cacheControl,
...(disableParallelToolUse !== undefined ? { disableParallelToolUse } : {}),
...(thinking !== undefined ? { thinking } : {}),
...(effort !== undefined ? { effort } : {}),
};
- return Object.keys(opts).length > 0 ? opts : undefined;
+ return opts;
}
/**
diff --git a/packages/harness/tests/core/llm/VercelAILLM.output.test.ts b/packages/harness/tests/core/llm/VercelAILLM.output.test.ts
index 5495c09c6..469e9fdb3 100644
--- a/packages/harness/tests/core/llm/VercelAILLM.output.test.ts
+++ b/packages/harness/tests/core/llm/VercelAILLM.output.test.ts
@@ -235,7 +235,7 @@ describe('buildProviderOptions', () => {
structuredOutputSpec: textSpec,
rawBody: {},
});
- expect(opts).toEqual({});
+ expect(opts).toEqual({ anthropic: { cacheControl: { type: 'ephemeral' } } });
}
});
@@ -248,6 +248,7 @@ describe('buildProviderOptions', () => {
rawBody: { thinking: { type: 'adaptive', display: 'summarized' }, effort: 'max' },
});
expect(opts['anthropic']).toEqual({
+ cacheControl: { type: 'ephemeral' },
thinking: { type: 'adaptive', display: 'summarized' },
effort: 'max',
});
@@ -260,7 +261,17 @@ describe('buildProviderOptions', () => {
structuredOutputSpec: schemaSpecStrict,
rawBody: {},
});
- expect(opts).toEqual({});
+ expect(opts).toEqual({ anthropic: { cacheControl: { type: 'ephemeral' } } });
+ });
+
+ it('defaults cacheControl to ephemeral when cache_control is omitted', () => {
+ const opts = buildProviderOptions({
+ config,
+ reasoningEffort: undefined,
+ structuredOutputSpec: textSpec,
+ rawBody: {},
+ });
+ expect(opts['anthropic']).toEqual({ cacheControl: { type: 'ephemeral' } });
});
it('forwards cache_control and disable_parallel_tool_use from rawBody', () => {
@@ -268,10 +279,10 @@ describe('buildProviderOptions', () => {
config,
reasoningEffort: undefined,
structuredOutputSpec: textSpec,
- rawBody: { cache_control: { type: 'ephemeral' }, disable_parallel_tool_use: true },
+ rawBody: { cache_control: { type: 'ephemeral', ttl: '1h' }, disable_parallel_tool_use: true },
});
expect(opts['anthropic']).toMatchObject({
- cacheControl: { type: 'ephemeral' },
+ cacheControl: { type: 'ephemeral', ttl: '1h' },
disableParallelToolUse: true,
});
});