Agent with Metrics

Code

1from kern.agent import Agent, RunOutput
2from kern.models.groq import Groq
3from kern.tools.yfinance import YFinanceTools
4from kern.utils.pprint import pprint_run_response
5from rich.pretty import pprint
6
7agent = Agent(
8 model=Groq(id="llama-3.3-70b-versatile"),
9 tools=[YFinanceTools(stock_price=True)],
10 markdown=True,
11)
12
13run_output: RunOutput = agent.run("What is the stock price of NVDA")
14pprint_run_response(run_output)
15
16# Print metrics per message
17if run_output.messages:
18 for message in run_output.messages: # type: ignore
19 if message.role == "assistant":
20 if message.content:
21 print(f"Message: {message.content}")
22 elif message.tool_calls:
23 print(f"Tool calls: {message.tool_calls}")
24 print("---" * 5, "Metrics", "---" * 5)
25 pprint(message.metrics)
26 print("---" * 20)
27
28# Print the metrics
29print("---" * 5, "Collected Metrics", "---" * 5)
30pprint(run_output.metrics) # type: ignore

Usage

Set up your virtual environment

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

Set your API key

1export GROQ_API_KEY=xxx

Install dependencies

1uv pip install -U groq yfinance kern-ai

Run Agent

1python cookbook/11_models/groq/metrics.py