mirror of
https://github.com/symfony/ai-anthropic-platform.git
synced 2026-03-23 23:32:07 +01:00
[Platform][Anthropic] Add prompt caching for tool definitions
Add cache_control marker to the last tool definition, creating an additional cache breakpoint between system prompt and messages. Tool definitions are identical across requests, making them an effective caching target that reduces input token costs. Respects the existing cacheRetention setting (none/short/long).
This commit is contained in:
@@ -63,6 +63,7 @@ final class ModelClient implements ModelClientInterface
|
||||
|
||||
if (isset($options['tools'])) {
|
||||
$options['tool_choice'] = ['type' => 'auto'];
|
||||
$options['tools'] = $this->injectToolsCacheControl($options['tools']);
|
||||
}
|
||||
|
||||
if (isset($options['thinking'])) {
|
||||
@@ -90,6 +91,33 @@ final class ModelClient implements ModelClientInterface
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects a prompt-caching marker on the last tool definition.
|
||||
*
|
||||
* This creates an additional cache breakpoint after all tool definitions,
|
||||
* so the prefix "system → tools" can be cached independently of the
|
||||
* messages that follow. Tool definitions are typically identical across
|
||||
* requests, making this a very effective caching target.
|
||||
*
|
||||
* @param list<array<string, mixed>> $tools Normalised tool definitions
|
||||
*
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function injectToolsCacheControl(array $tools): array
|
||||
{
|
||||
if ('none' === $this->cacheRetention || [] === $tools) {
|
||||
return $tools;
|
||||
}
|
||||
|
||||
$cacheControl = 'long' === $this->cacheRetention
|
||||
? ['type' => 'ephemeral', 'ttl' => '1h']
|
||||
: ['type' => 'ephemeral'];
|
||||
|
||||
$tools[\count($tools) - 1]['cache_control'] = $cacheControl;
|
||||
|
||||
return $tools;
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects prompt-caching markers into the normalised message payload.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user