Agent Extra Metrics

Access audio, cache, and reasoning token counts from agent runs.

Code

1import requests
2from kern.agent import Agent
3from kern.media import Audio
4from kern.models.openai import OpenAIChat, OpenAIResponses
5from kern.utils.pprint import pprint_run_response
6
7# Fetch the audio file and convert it to a base64 encoded string
8url = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav"
9response = requests.get(url)
10response.raise_for_status()
11wav_data = response.content
12
13agent = Agent(
14 model=OpenAIChat(
15 id="gpt-4o-audio-preview",
16 modalities=["text", "audio"],
17 audio={"voice": "sage", "format": "wav"},
18 ),
19 markdown=True,
20)
21run_response = agent.run(
22 "What's in this recording?",
23 audio=[Audio(content=wav_data, format="wav")],
24)
25pprint_run_response(run_response)
26# Showing input audio and output audio tokens metrics
27print(f"Input audio tokens: {run_response.metrics.audio_input_tokens}")
28print(f"Output audio tokens: {run_response.metrics.audio_output_tokens}")
29
30agent = Agent(
31 model=OpenAIResponses(id="gpt-5.2"),
32 markdown=True,
33 telemetry=False,
34)
35run_response = agent.run(
36 "Solve the trolley problem. Evaluate multiple ethical frameworks. Include an ASCII diagram of your solution.",
37 stream=False,
38)
39pprint_run_response(run_response)
40# Showing reasoning tokens metrics
41print(f"Reasoning tokens: {run_response.metrics.reasoning_tokens}")
42
43agent = Agent(model=OpenAIResponses(id="gpt-5.2"), markdown=True, telemetry=False)
44agent.run("Share a 2 sentence horror story" * 150)
45run_response = agent.run("Share a 2 sentence horror story" * 150)
46# Showing cached tokens metrics
47print(f"Cached tokens: {run_response.metrics.cache_read_tokens}")

Usage

Create a Python file

Create agent_extra_metrics.py with the code above.

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Install dependencies

1uv pip install -U kern-ai openai requests

Export your OpenAI API key

1export OPENAI_API_KEY="your_openai_api_key_here"
1$Env:OPENAI_API_KEY="your_openai_api_key_here"

Run Agent

1python agent_extra_metrics.py