LiteLLM Metrics
Inspect per-message and run-level token metrics from a LiteLLM agent using YFinance tools.
"""
Litellm Metrics
===============
Cookbook example for `litellm/metrics.py`.
"""
from agno.agent import Agent, RunOutput
from agno.models.litellm import LiteLLM
from agno.tools.yfinance import YFinanceTools
from agno.utils.pprint import pprint_run_response
from rich.pretty import pprint
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=LiteLLM(
id="gpt-5.6-luna",
),
tools=[YFinanceTools()],
markdown=True,
)
run_output: RunOutput = agent.run("What is the stock price of NVDA")
pprint_run_response(run_output, markdown=True)
# Print metrics per message
if run_output.messages:
for message in run_output.messages:
if message.role == "assistant":
if message.content:
print(f"Message: {message.content}")
elif message.tool_calls:
print(f"Tool calls: {message.tool_calls}")
print("---" * 5, "Metrics", "---" * 5)
pprint(message.metrics)
print("---" * 20)
# Print the metrics
print("---" * 5, "Collected Metrics", "---" * 5)
pprint(run_output.metrics) # type: ignore
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
passRun the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno litellm yfinanceSet your OpenAI credentials
Use an OpenAI API key with access to the requested model. The LiteLLM SDK calls the provider directly. An existing LITELLM_API_KEY overrides provider-specific credentials, so clear it for this example.
unset LITELLM_API_KEY
export OPENAI_API_KEY="your_provider_api_key_here"Set compatible sampling options
Add temperature=None, top_p=None to every LiteLLM(...) using id="gpt-5.6-luna" or id="openai/gpt-5.6-luna" in your saved file. The adapter defaults to temperature=0.7 and top_p=1.0; the LiteLLM SDK rejects those sampling settings for this model's default reasoning mode before sending a request.
Run the example
Save the code above as metrics.py, then run:
python metrics.pyFull source: cookbook/90_models/litellm/metrics.py