Multi-DB Tracing with setup_tracing()

Trace agents with multiple databases using setup_tracing() in AgentOS.

This example shows how to configure tracing when agents have separate databases using setup_tracing(). A dedicated tracing database ensures all traces are stored in one central location.

Create a Python file

1from kern.agent import Agent
2from kern.db.sqlite import SqliteDb
3from kern.models.openai import OpenAIResponses
4from kern.os import AgentOS
5from kern.tools.hackernews import HackerNewsTools
6from kern.tools.hackernews import HackerNewsTools
7from kern.tracing import setup_tracing
8
9# Set up databases - each agent has its own db
10db1 = SqliteDb(db_file="tmp/db1.db", id="db1")
11db2 = SqliteDb(db_file="tmp/db2.db", id="db2")
12
13# Dedicated traces database
14db = SqliteDb(db_file="tmp/traces.db", id="traces")
15
16# Setup tracing with custom configuration
17setup_tracing(
18 db=db,
19 batch_processing=True,
20 max_queue_size=1024,
21 max_export_batch_size=256,
22)
23
24agent = Agent(
25 name="HackerNews Agent",
26 model=OpenAIResponses(id="gpt-5.2"),
27 tools=[HackerNewsTools()],
28 instructions="You are a hacker news agent. Answer questions concisely.",
29 markdown=True,
30 db=db1,
31)
32
33agent2 = Agent(
34 name="Web Search Agent",
35 model=OpenAIResponses(id="gpt-5.2"),
36 tools=[HackerNewsTools()],
37 instructions="You are a web search agent. Answer questions concisely.",
38 markdown=True,
39 db=db2,
40)
41
42# Setup AgentOS with dedicated db
43# This ensures traces are written to and read from the same database
44agent_os = AgentOS(
45 description="Example app for tracing with multiple databases",
46 agents=[agent, agent2],
47 db=db, # Dedicated database for traces
48)
49app = agent_os.get_app()
50
51if __name__ == "__main__":
52 agent_os.serve(app="tracing_multi_db_setup:app", reload=True)

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 openai kern-ai opentelemetry-api opentelemetry-sdk openinference-instrumentation-kern-ai

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 AgentOS

1python tracing_multi_db_setup.py

Your AgentOS will be available at http://localhost:7777. View traces in the AgentOS dashboard.