Skip to content

Commit 286acc5

Browse files
committed
Decouple Platform Token Usage handling from Agent by introducing new interface
1 parent b9a7258 commit 286acc5

File tree

79 files changed

+953
-1237
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+953
-1237
lines changed

deptrac.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,4 @@ deptrac:
306306
- PlatformComponent
307307
# Baseline of known violations to be skipped for now
308308
skip_violations:
309-
Symfony\AI\Platform\Bridge\Anthropic\TokenOutputProcessor: [Symfony\AI\Agent\OutputProcessorInterface, Symfony\AI\Agent\Output]
310-
Symfony\AI\Platform\Bridge\DeepSeek\TokenOutputProcessor: [Symfony\AI\Agent\OutputProcessorInterface, Symfony\AI\Agent\Output]
311-
Symfony\AI\Platform\Bridge\Gemini\TokenOutputProcessor: [Symfony\AI\Agent\OutputProcessorInterface, Symfony\AI\Agent\Output]
312-
Symfony\AI\Platform\Bridge\Mistral\TokenOutputProcessor: [Symfony\AI\Agent\OutputProcessorInterface, Symfony\AI\Agent\Output]
313-
Symfony\AI\Platform\Bridge\OpenAi\TokenOutputProcessor: [Symfony\AI\Agent\OutputProcessorInterface, Symfony\AI\Agent\Output]
314309
Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor: [Symfony\AI\Agent\OutputProcessorInterface, Symfony\AI\Agent\Output]
315-
Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor: [Symfony\AI\Agent\OutputProcessorInterface, Symfony\AI\Agent\Output]
316-
Symfony\AI\Platform\Bridge\VertexAi\TokenOutputProcessor: [Symfony\AI\Agent\OutputProcessorInterface, Symfony\AI\Agent\Output]

docs/bundles/ai-bundle.rst

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ Advanced Example with Multiple Agents
6767
agent:
6868
rag:
6969
platform: 'ai.platform.azure.gpt_deployment'
70-
track_token_usage: true # Enable tracking of token usage for the agent, default is true
7170
model: 'gpt-4o-mini'
7271
memory: 'You have access to conversation history and user preferences' # Optional: static memory content
7372
prompt: # The system prompt configuration
@@ -899,8 +898,8 @@ Token Usage Tracking
899898
Token usage tracking is a feature provided by some of the Platform's bridges, for monitoring and analyzing the
900899
consumption of tokens by your agents. This feature is particularly useful for understanding costs and performance.
901900

902-
When enabled, the agent will automatically track token usage information and add it
903-
to the result metadata. The tracked information includes:
901+
In case a Platform bridge supports token usage tracking, the Platform will automatically track token usage information
902+
and add it to the result metadata. The tracked information includes:
904903

905904
* **Prompt tokens**: Number of tokens used in the input/prompt
906905
* **Completion tokens**: Number of tokens generated in the response
@@ -932,19 +931,6 @@ The token usage information can be accessed from the result metadata::
932931
}
933932
}
934933

935-
Disable Tracking
936-
~~~~~~~~~~~~~~~~
937-
938-
To disable token usage tracking for an agent, set the ``track_token_usage`` option to ``false``:
939-
940-
.. code-block:: yaml
941-
942-
ai:
943-
agent:
944-
my_agent:
945-
model: 'gpt-4o-mini'
946-
track_token_usage: false
947-
948934
Vectorizers
949935
-----------
950936

examples/anthropic/token-metadata.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
use Symfony\AI\Agent\Agent;
1313
use Symfony\AI\Platform\Bridge\Anthropic\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\Anthropic\TokenOutputProcessor;
1514
use Symfony\AI\Platform\Message\Message;
1615
use Symfony\AI\Platform\Message\MessageBag;
1716

1817
require_once dirname(__DIR__).'/bootstrap.php';
1918

2019
$platform = PlatformFactory::create(env('ANTHROPIC_API_KEY'), http_client());
2120

22-
$agent = new Agent($platform, 'claude-sonnet-4-5-20250929', outputProcessors: [new TokenOutputProcessor()]);
21+
$agent = new Agent($platform, 'claude-sonnet-4-5-20250929');
2322
$messages = new MessageBag(
2423
Message::forSystem('You are a pirate and you write funny.'),
2524
Message::ofUser('What is the Symfony framework?'),

examples/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use Symfony\AI\Agent\Exception\ExceptionInterface as AgentException;
1515
use Symfony\AI\Platform\Exception\ExceptionInterface as PlatformException;
1616
use Symfony\AI\Platform\Metadata\Metadata;
17-
use Symfony\AI\Platform\Metadata\TokenUsage;
1817
use Symfony\AI\Platform\Result\DeferredResult;
18+
use Symfony\AI\Platform\TokenUsage\TokenUsage;
1919
use Symfony\AI\Store\Exception\ExceptionInterface as StoreException;
2020
use Symfony\Component\Console\Helper\Table;
2121
use Symfony\Component\Console\Logger\ConsoleLogger;

examples/deepseek/token-metadata.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
use Symfony\AI\Agent\Agent;
1313
use Symfony\AI\Platform\Bridge\DeepSeek\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\DeepSeek\TokenOutputProcessor;
1514
use Symfony\AI\Platform\Message\Message;
1615
use Symfony\AI\Platform\Message\MessageBag;
1716

1817
require_once dirname(__DIR__).'/bootstrap.php';
1918

2019
$platform = PlatformFactory::create(env('DEEPSEEK_API_KEY'), http_client());
2120

22-
$agent = new Agent($platform, 'deepseek-chat', outputProcessors: [new TokenOutputProcessor()]);
21+
$agent = new Agent($platform, 'deepseek-chat');
2322
$messages = new MessageBag(
2423
Message::forSystem('You are a pirate and you write funny.'),
2524
Message::ofUser('What is the Symfony framework?'),

examples/gemini/token-metadata.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
use Symfony\AI\Agent\Agent;
1313
use Symfony\AI\Platform\Bridge\Gemini\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\Gemini\TokenOutputProcessor;
1514
use Symfony\AI\Platform\Message\Message;
1615
use Symfony\AI\Platform\Message\MessageBag;
1716

1817
require_once dirname(__DIR__).'/bootstrap.php';
1918

2019
$platform = PlatformFactory::create(env('GEMINI_API_KEY'), http_client());
2120

22-
$agent = new Agent($platform, 'gemini-2.0-flash', outputProcessors: [new TokenOutputProcessor()]);
21+
$agent = new Agent($platform, 'gemini-2.0-flash');
2322
$messages = new MessageBag(
2423
Message::forSystem('You are a pirate and you write funny.'),
2524
Message::ofUser('What is the Symfony framework?'),

examples/mistral/token-metadata.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
use Symfony\AI\Agent\Agent;
1313
use Symfony\AI\Platform\Bridge\Mistral\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\Mistral\TokenOutputProcessor;
1514
use Symfony\AI\Platform\Message\Message;
1615
use Symfony\AI\Platform\Message\MessageBag;
1716

1817
require_once dirname(__DIR__).'/bootstrap.php';
1918

2019
$platform = PlatformFactory::create(env('MISTRAL_API_KEY'), http_client());
2120

22-
$agent = new Agent($platform, 'mistral-large-latest', outputProcessors: [new TokenOutputProcessor()]);
21+
$agent = new Agent($platform, 'mistral-large-latest');
2322

2423
$messages = new MessageBag(
2524
Message::forSystem('You are a pirate and you write funny.'),

examples/openai/token-metadata.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
use Symfony\AI\Agent\Agent;
1313
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\OpenAi\TokenOutputProcessor;
1514
use Symfony\AI\Platform\Message\Message;
1615
use Symfony\AI\Platform\Message\MessageBag;
1716

1817
require_once dirname(__DIR__).'/bootstrap.php';
1918

2019
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
2120

22-
$agent = new Agent($platform, 'gpt-4o-mini', outputProcessors: [new TokenOutputProcessor()]);
21+
$agent = new Agent($platform, 'gpt-4o-mini');
2322
$messages = new MessageBag(
2423
Message::forSystem('You are a pirate and you write funny.'),
2524
Message::ofUser('What is the Symfony framework?'),

examples/perplexity/token-metadata.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111

1212
use Symfony\AI\Agent\Agent;
1313
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor;
1514
use Symfony\AI\Platform\Message\Message;
1615
use Symfony\AI\Platform\Message\MessageBag;
1716

1817
require_once dirname(__DIR__).'/bootstrap.php';
1918

2019
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
21-
$agent = new Agent($platform, 'sonar', outputProcessors: [new TokenOutputProcessor()]);
20+
$agent = new Agent($platform, 'sonar');
2221

2322
$messages = new MessageBag(
2423
Message::forSystem('You are a pirate and you write funny.'),

examples/vertexai/token-metadata.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
use Symfony\AI\Agent\Agent;
1313
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\VertexAi\TokenOutputProcessor;
1514
use Symfony\AI\Platform\Message\Message;
1615
use Symfony\AI\Platform\Message\MessageBag;
1716

1817
require_once __DIR__.'/bootstrap.php';
1918

2019
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
2120

22-
$agent = new Agent($platform, 'gemini-2.0-flash-lite', outputProcessors: [new TokenOutputProcessor()]);
21+
$agent = new Agent($platform, 'gemini-2.0-flash-lite');
2322
$messages = new MessageBag(
2423
Message::forSystem('You are an expert assistant in animal study.'),
2524
Message::ofUser('What does a cat usually eat?'),

0 commit comments

Comments
 (0)