-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat!: Track step_count, token_usage and tool_call_counts in Agent's State
#11427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a295ab
Add internal keys to State to track useful info
sjrl 3534a96
add reno
sjrl 77d68ce
Updates
sjrl 34b8e19
add more unit tests
sjrl c5bc2ae
Update integration tests and update LLM to account for new Agent outputs
sjrl 22901c9
PR comments
sjrl 4295c8c
Update test/components/agents/test_agent.py
sjrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,13 @@ def __init__( | |
| ) | ||
| component.set_input_type(self, "messages", list[ChatMessage], None) | ||
|
|
||
| # The Agent base class declares `step_count` and `tool_call_counts` as outputs, but an LLM never has tools | ||
| # and always runs exactly one step — those values are uninformative, so drop them from the public surface. | ||
| # `token_usage` is still meaningful and stays exposed. | ||
| component.set_output_types( | ||
| self, messages=list[ChatMessage], last_message=ChatMessage, token_usage=dict[str, Any] | ||
| ) | ||
|
|
||
| def to_dict(self) -> dict[str, Any]: | ||
| """ | ||
| Serialize the LLM component to a dictionary. | ||
|
|
@@ -140,16 +147,22 @@ def run( # type: ignore[override] # `messages` is in **kwargs to allow dynamic | |
| A dictionary with the following keys: | ||
| - "messages": List of all messages exchanged during the LLM's run. | ||
| - "last_message": The last message exchanged during the LLM's run. | ||
| - "token_usage": Token usage from the LLM call (e.g. prompt_tokens, completion_tokens). Empty if the | ||
| chat generator did not return usage data. | ||
| """ | ||
| # `messages` is intentionally omitted from the signature so the framework can treat it as required | ||
| # or optional depending on init configuration. See __init__ for details. | ||
| messages = kwargs.pop("messages", None) | ||
| return super(LLM, self).run( # noqa: UP008 | ||
| result = super(LLM, self).run( # noqa: UP008 | ||
| messages=messages or [], | ||
| streaming_callback=streaming_callback, | ||
| generation_kwargs=generation_kwargs, | ||
| **kwargs, | ||
| ) | ||
| # Inherited Agent-internal bookkeeping that isn't useful at the LLM surface. | ||
| result.pop("step_count", None) | ||
| result.pop("tool_call_counts", None) | ||
| return result | ||
|
|
||
| async def run_async( # type: ignore[override] # `messages` is in **kwargs to allow dynamic required/optional status | ||
| self, | ||
|
|
@@ -174,13 +187,19 @@ async def run_async( # type: ignore[override] # `messages` is in **kwargs to a | |
| A dictionary with the following keys: | ||
| - "messages": List of all messages exchanged during the LLM's run. | ||
| - "last_message": The last message exchanged during the LLM's run. | ||
| - "token_usage": Token usage from the LLM call (e.g. prompt_tokens, completion_tokens). Empty if the | ||
| chat generator did not return usage data. | ||
| """ | ||
| # `messages` is intentionally omitted from the signature so the framework can treat it as required | ||
| # or optional depending on init configuration. See __init__ for details. | ||
| messages = kwargs.pop("messages", None) | ||
| return await super(LLM, self).run_async( # noqa: UP008 | ||
| result = await super(LLM, self).run_async( # noqa: UP008 | ||
| messages=messages or [], | ||
| streaming_callback=streaming_callback, | ||
| generation_kwargs=generation_kwargs, | ||
| **kwargs, | ||
| ) | ||
| # Inherited Agent-internal bookkeeping that isn't useful at the LLM surface. | ||
| result.pop("step_count", None) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we add another key to the Agent's |
||
| result.pop("tool_call_counts", None) | ||
| return result | ||
21 changes: 21 additions & 0 deletions
21
releasenotes/notes/expose-agent-run-metadata-8f2942aba14b4f0c.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --- | ||
| enhancements: | ||
| - | | ||
| ``Agent`` now exposes three new outputs that are populated automatically during a | ||
| run and made available alongside ``messages`` and ``last_message`` in the result dict: | ||
|
|
||
| - ``step_count`` (``int``): the number of steps the agent ran. | ||
| - ``token_usage`` (``dict[str, Any]``): aggregated token usage summed across every LLM call in the run | ||
| - ``tool_call_counts`` (``dict[str, int]``): number of times each tool was invoked, keyed by tool name. | ||
|
|
||
| These fields are added to ``Agent.state_schema`` automatically so that tools registered via ``inputs_from_state`` can read them mid-run. | ||
| They are exposed only as Agent outputs so cannot be passed in as inputs to ``Agent.run`` / ``Agent.run_async``. | ||
| - | | ||
| ``LLM`` now exposes a ``token_usage`` output alongside ``messages`` and ``last_message``. Because ``LLM`` never | ||
| invokes tools and always runs exactly one step, ``step_count`` and ``tool_call_counts`` inherited from ``Agent`` | ||
| are not exposed on ``LLM``. | ||
| upgrade: | ||
| - | | ||
| ``step_count``, ``token_usage``, and ``tool_call_counts`` are now reserved keys in ``Agent.state_schema``. | ||
| Passing any of them via the ``state_schema`` argument now raises ``ValueError``. | ||
| Rename the conflicting state key (e.g. ``my_token_usage``) to migrate. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about adding this to the release note under enhancements? I suggest we do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 22901c9