generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 718
Open
Labels
area-providerRelated to model providersRelated to model providersenhancementNew feature or requestNew feature or requestready for contributionPull requests welcomePull requests welcome
Description
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:
- Transforms Strands agent result to LiteLLM response format
- Calls
litellm.completion_cost()to calculate the cost - Includes the cost in
AgentResultmetadata
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-providerRelated to model providersRelated to model providersenhancementNew feature or requestNew feature or requestready for contributionPull requests welcomePull requests welcome