Skip to content

[FEATURE] Add LiteLLM Cost Tracking to Agent Results #1216

@iyamabhishek

Description

@iyamabhishek

Problem Statement

Summary

Add support for returning LiteLLM cost data as part of AgentResult metadata when using LiteLLMModel.

Motivation

Currently, Strands agents track token usage via accumulated_usage, but there's no direct way to get the dollar cost of model invocations. LiteLLM provides a completion_cost() function that calculates costs based on token usage and model pricing, but this isn't exposed through the Strands SDK.

Proposed Solution

Proposed Solution

Add a pass-through function in Strands' LiteLLM model provider (strands/models/litellm.py) that:

  1. Transforms Strands agent result to LiteLLM response format
  2. Calls litellm.completion_cost() to calculate the cost
  3. Includes the cost in AgentResult metadata

Example Usage

from strands import Agent
from strands.models.litellm import LiteLLMModel

model = LiteLLMModel(
    model_id="anthropic/claude-3-7-sonnet-20250219"
)

agent = Agent(model=model)
response = agent("What is 2+2")

# Access cost from response metadata
print(f"Cost: ${response.metadata.cost:.10f}")

Implementation Notes

The completion_cost function in LiteLLM has provider-specific logic for each model, so this would leverage existing LiteLLM functionality rather than reimplementing cost calculation.

Use Case

Users need cost tracking for:

  • Budget monitoring and cost optimization
  • Per-request cost analysis
  • Comparing costs across different models
  • Production cost tracking and reporting

Alternatives Solutions

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions