Agent Extra Metrics
Access audio, cache, and reasoning token counts from agent runs.
Code
1import requests2from kern.agent import Agent3from kern.media import Audio4from kern.models.openai import OpenAIChat, OpenAIResponses5from kern.utils.pprint import pprint_run_response67# Fetch the audio file and convert it to a base64 encoded string8url = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav"9response = requests.get(url)10response.raise_for_status()11wav_data = response.content1213agent = 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 metrics27print(f"Input audio tokens: {run_response.metrics.audio_input_tokens}")28print(f"Output audio tokens: {run_response.metrics.audio_output_tokens}")2930agent = 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 metrics41print(f"Reasoning tokens: {run_response.metrics.reasoning_tokens}")4243agent = 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 metrics47print(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.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U kern-ai openai requestsExport 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