#1604 Track LLM and embedding cost per individual call - #1663
Merged
Conversation
Signed-off-by: Charles <azanux@gmail.com>
igordayen
reviewed
May 9, 2026
igordayen
left a comment
Contributor
There was a problem hiding this comment.
looks good! few comments to consider, thanks!
…ti-line syntax fix Signed-off-by: Charles <azanux@gmail.com>
igordayen
approved these changes
May 9, 2026
igordayen
left a comment
Contributor
There was a problem hiding this comment.
good, please mark comments as resolved.
dearshor
pushed a commit
to dearshor/embabel-agent
that referenced
this pull request
Jun 29, 2026
…l#1663) * embabel#1604 Track LLM and embedding cost per individual call Signed-off-by: Charles <azanux@gmail.com> * embabel#1604 Track LLM and embedding cost per individual call - - milti-line syntax fix Signed-off-by: Charles <azanux@gmail.com> --------- Signed-off-by: Charles <azanux@gmail.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR closes the observability part of #1604. The goal is to track cost per individual LLM call.
Today, usage is recorded once per tool-loop as a single aggregate. This breaks down in
CONCURRENTmode . The ticket needsLlmMetadata,Usage,interactionIdandagentProcess.idavailable for each call.The PR adds two new events:
LlmInvocationEventandEmbeddingInvocationEvent. Each fires once per call, with the four required fields. Listeners can aggregate cost as they need.The implementation reuses an existing extension point. A
ToolLoopInspectoris created inline at eachtoolLoopFactory.create(...)site inToolLoopLlmOperations. It capturesllmandllmRequestEventvia closure.DefaultToolLoopfires it through itsafterLlmCallhook. The tool-loop core is untouched.Small side refactor:
doTransformIfPossiblewas inlining start/completed event logic. It now uses the sharedpublishToolLoopStartEventandhandleToolLoopCompletionhelpers, like the three otherdoTransform*methods. This addresses the "seems one call is missing" review comment of @igordayenThe post-loop
recordUsage(...)was removed fromhandleToolLoopCompletion. Keeping it would double-count on top of the per-call inspector.EmbeddingOperationsemitsEmbeddingInvocationEventafter each recorded invocation, same pattern.Behavior change
agentProcess.llmInvocationsnow contains N entries per loop (one per call) instead of one aggregate. Sums (cost(),usage()) are unchanged. Tests assertingsize == 1on loop scenarios were updated. No production caller depends on the aggregate semantics.Sample listener
Not in this PR
Pre-call enforcement #1597 (blocking when a usage limit is hit) is out of scope.but this feature solve it.
Streaming is deferred. Stefan confirmed it isn't needed for v0.5.
Observability module - a full update will be handled in a dedicated PR to account for the changes introduced here and handle in the same time issue #1561 and #1588
Test plan
LlmInvocationEventTest/EmbeddingInvocationEventTest- event shape and fieldsChatClientLlmTransformerTest- per-call count + matchinginteractionIdEmbeddingOperationsTest- emission with/without active agentToolLoopLlmOperationsTest.BillingInspectorTests- per-call emission across the 4doTransform*paths, listener-cost aggregation, concurrency (16 parallel calls produce 16 distinct events with no cross-threadinteractionIdbleed), and listener-exception isolation (loop survives a listener that throws on everyLlmInvocationEvent)LLMOpenAiCostTrackingIT- end-to-end IT with a real OpenAI callembabel-agent-apisuite green: 3530 tests, 0 failures