Skip to content

Add token usage tracking to call_with_tools() #2

Closed

Description

@AmitoVrito

Description

The call_with_tools() method in OpenAI, Anthropic, Gemini, and Mistral providers doesn't track _input_tokens / _output_tokens like stream() does.

Details

  • Update call_with_tools() in each provider to read token usage from the API response
  • OpenAI/Mistral: response.usage.prompt_tokens / response.usage.completion_tokens
  • Anthropic: response.usage.input_tokens / response.usage.output_tokens
  • Gemini: response.usage_metadata.prompt_token_count / response.usage_metadata.candidates_token_count
  • Add tests verifying llm.tokens_used is updated after call_with_tools()

Reference

Look at how stream() updates self._input_tokens and self._output_tokens in each provider file under src/synapsekit/llm/.

Activity

  1. added
    enhancement
    good first issue
    on Mar 12, 2026
  2. AmitoVrito commented on Mar 12, 2026

    @AmitoVrito
    CollaboratorAuthor

    Thanks for your interest! I've assigned this to you. Before you start, could you give the repo a ⭐? It really helps with visibility for a new project. Looking forward to your PR!

  3. passionworkeer commented on Mar 12, 2026

    @passionworkeer

    A clean way to do this is to reuse the same accounting path as stream(): add a small provider-local helper like _apply_usage(response) and call it at the end of call_with_tools(). That keeps OpenAI/Mistral/Anthropic/Gemini parsing differences isolated while guaranteeing llm.tokens_used stays consistent across sync + streaming paths. I’d also add one regression test per provider with a minimal mocked response object, so future SDK field changes fail fast.

  4. AmitoVrito commented on Mar 12, 2026

    @AmitoVrito
    CollaboratorAuthor

    Thanks for your interest! I will assign it to you. Before you start, could you give the repo a ⭐? It really helps with visibility for a new project. Looking forward to your PR!

  5. AmitoVrito commented on Mar 12, 2026

    @AmitoVrito
    CollaboratorAuthor

    so in our Current state OpenAI's call_with_tools() already tracks tokens (lines 80-81), but looking at the other providers:

    • OpenAI — tracks tokens in call_with_tools() ✅
    • Anthropic, Gemini, Mistral — likely don't track tokens in call_with_tools() (they do in stream() but not the non-streaming tool call path)

    The suggestion is sound:

    1. _apply_usage(response) helper — each provider parses usage differently (OpenAI: response.usage.prompt_tokens, Anthropic:response.usage.input_tokens, Gemini: response.usage_metadata.prompt_token_count). A per-provider helper keeps that parsing isolated and DRY betweenstream() and call_with_tools().
    2. One test per provider with mocked response iis exactly the right approach, catches SDK field changes early.
    3. Keeps tokens_used consistent: right now an agent using call_with_tools() on Gemini/Mistral/Anthropic would show 0 tokens, which is a bug.

    i welcome this PR!!

  6. AmitoVrito commented on Mar 20, 2026

    @AmitoVrito
    CollaboratorAuthor

    Resolved in v1.2.0 with CostTracker (hierarchical cost attribution with scope context manager) and BudgetGuard (budget limits with circuit breaker). See #316.

  7. added a commit that references this issue on Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementgood first issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions