Skip to content

Building AI agents, atomically

Add Phoenix tracing example

Eigenwise9b8f0b064d07
diff --git a/README.md b/README.md
index 4e78b32..a8e1e92 100644
--- a/README.md
+++ b/README.md
@@ -287,6 +287,7 @@ For full, runnable examples, please refer to the following files in the `atomic-
In addition to the quickstart examples, we have more complex examples demonstrating the power of Atomic Agents:
- [Hooks System](/atomic-examples/hooks-example/README.md): Comprehensive demonstration of the AtomicAgent hook system for monitoring, error handling, and performance metrics with intelligent retry mechanisms.
+- [Phoenix Tracing](/atomic-examples/phoenix-tracing/README.md): Traces agents and tools in Arize Phoenix through the optional OpenInference instrumentor (`pip install "atomic-agents[observability]"`).
- [Basic Multimodal](/atomic-examples/basic-multimodal/README.md): Demonstrates how to analyze images with text, focusing on extracting structured information from nutrition labels using GPT-4 Vision capabilities.
- [Deep Research](/atomic-examples/deep-research/README.md): An advanced example showing how to perform deep research tasks.
- [Orchestration Agent](/atomic-examples/orchestration-agent/README.md): Shows how to create an Orchestrator Agent that intelligently decides between using different tools (search or calculator) based on user input.
diff --git a/atomic-examples/phoenix-tracing/README.md b/atomic-examples/phoenix-tracing/README.md
new file mode 100644
index 0000000..077801d
--- /dev/null
+++ b/atomic-examples/phoenix-tracing/README.md
@@ -0,0 +1,50 @@
+# Phoenix Tracing Example
+
+Trace an Atomic Agents turn in [Arize Phoenix](https://github.com/Arize-ai/phoenix): a planner agent writes
+an arithmetic expression, a calculator tool evaluates it, and an answer agent explains the result. One run
+produces this tree in Phoenix:
+
+```
+CHAIN answer_question
+├── AGENT AtomicAgent[BasicChatInputSchema, CalculationPlanSchema].run
+│ └── LLM ChatCompletion (from openinference-instrumentation-openai)
+├── TOOL CalculatorTool
+└── AGENT AtomicAgent[CalculationResultSchema, BasicChatOutputSchema].run
+ └── LLM ChatCompletion
+```
+
+The AGENT and TOOL spans come from `atomic_agents.observability`, which Phoenix activates automatically
+through `register(auto_instrument=True)`. The LLM spans come from the OpenAI instrumentor. The CHAIN span
+is the only manual tracing in `main.py`; without it every agent and tool call would be its own trace.
+
+## Run it
+
+1. Start Phoenix locally (or point `PHOENIX_COLLECTOR_ENDPOINT` at a hosted instance):
+
+ ```bash
+ uv run --with arize-phoenix phoenix serve
+ ```
+
+ Phoenix listens on http://localhost:6006.
+
+2. Put `OPENAI_API_KEY=...` in a `.env` file here, then run the example from this directory:
+
+ ```bash
+ uv sync
+ uv run python phoenix_tracing/main.py
+ ```
+
+3. Open http://localhost:6006, pick the `atomic-agents-example` project, and open the latest trace.
+
+## Environment
+
+| Variable | Purpose |
+|----------|---------|
+| `OPENAI_API_KEY` | Required. |
+| `OPENAI_MODEL` | Optional, defaults to `gpt-5-mini`. |
+| `PHOENIX_COLLECTOR_ENDPOINT` | Where spans go. Unset, `register()` uses gRPC to `localhost:4317`; `http://localhost:6006` sends HTTP to the UI port. |
+| `PHOENIX_API_KEY` | Only for Phoenix Cloud or an authenticated server. |
+| `OPENINFERENCE_HIDE_INPUTS` / `OPENINFERENCE_HIDE_OUTPUTS` | Set to `true` to redact agent and tool payloads. |
+
+The full setup guide, including manual OpenTelemetry setup and privacy controls, is in
+`docs/guides/observability.md`.
diff --git a/atomic-examples/phoenix-tracing/phoenix_tracing/main.py b/atomic-examples/phoenix-tracing/phoenix_tracing/main.py
new file mode 100644
index 0000000..ae1f9a3
--- /dev/null
+++ b/atomic-examples/phoenix-tracing/phoenix_tracing/main.py
@@ -0,0 +1,99 @@
+import os
+import uuid
+
+import instructor
+import openai
+from dotenv import load_dotenv
+from openinference.instrumentation import using_session
+from phoenix.otel import register
+from pydantic import Field
+from rich.console import Console
+
+from atomic_agents import AgentConfig, AtomicAgent, BaseIOSchema, BasicChatInputSchema, BasicChatOutputSchema
+from atomic_agents.context import SystemPromptGenerator
+from phoenix_tracing.tools.calculator import CalculatorTool, CalculatorToolInputSchema
+
+load_dotenv()
+
+tracer_provider = register(project_name="atomic-agents-example", auto_instrument=True)
+tracer = tracer_provider.get_tracer(__name__)
+console = Console()
+
+
+class CalculationPlanSchema(BaseIOSchema):
+ """A single arithmetic expression that answers the user's question."""
+
+ expression: str = Field(..., description="A sympy-compatible arithmetic expression, for example '12 * 7 + 3'.")
+ reasoning: str = Field(..., description="One sentence on why this expression answers the question.")
+
+
+class CalculationResultSchema(BaseIOSchema):
+ """The user's question together with the evaluated expression."""
+
+ question: str = Field(..., description="The original question.")
+ expression: str = Field(..., description="The expression that was evaluated.")
+ result: str = Field(..., description="The evaluated result.")
+
+
+def build_client() -> instructor.Instructor:
+ return instructor.from_openai(openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY")))
+
+
+def build_planner(client: instructor.Instructor) -> AtomicAgent[BasicChatInputSchema, CalculationPlanSchema]:
+ return AtomicAgent[BasicChatInputSchema, CalculationPlanSchema](
+ AgentConfig(
+ client=client,
+ model=os.getenv("OPENAI_MODEL", "gpt-5-mini"),
+ system_prompt_generator=SystemPromptGenerator(
+ background=["You turn word problems into one arithmetic expression a calculator can evaluate."],
+ output_instructions=["Use only numbers and the operators + - * / ** ( )."],
+ ),
+ )
+ )
+
+
+def build_answerer(client: instructor.Instructor) -> AtomicAgent[CalculationResultSchema, BasicChatOutputSchema]:
+ return AtomicAgent[CalculationResultSchema, BasicChatOutputSchema](
+ AgentConfig(
+ client=client,
+ model=os.getenv("OPENAI_MODEL", "gpt-5-mini"),
+ system_prompt_generator=SystemPromptGenerator(
+ background=["You explain a computed result to the user in one friendly sentence."],
+ ),
+ )
+ )
+
+
+def answer_question(question: str) -> str:
+ client = build_client()
+ planner = build_planner(client)
+ calculator = CalculatorTool()
+ answerer = build_answerer(client)
+
+ with tracer.start_as_current_span("answer_question", openinference_span_kind="chain") as turn:
+ turn.set_input(question)
+ plan = planner.run(BasicChatInputSchema(chat_message=question))
+ calculation = calculator.run(CalculatorToolInputSchema(expression=plan.expression))
+ answer = answerer.run(
+ CalculationResultSchema(question=question, expression=plan.expression, result=calculation.result)
+ )
+ turn.set_output(answer.chat_message)
+ return answer.chat_message
+
+
+def main() -> None:
+ question = "If 3 friends split a 47 euro bill and add a 15 percent tip, how much does each pay?"
+ with using_session(session_id=str(uuid.uuid4())):
+ answer = answer_question(question)
+
+ console.print(f"[bold green]Question:[/bold green] {question}")
+ console.print(f"[bold blue]Answer:[/bold blue] {answer}")
+ console.print(
+ "\nOpen Phoenix at "
+ f"[link]{os.getenv('PHOENIX_COLLECTOR_ENDPOINT', 'http://localhost:6006')}[/link] "
+ "and look at the 'atomic-agents-example' project."
+ )
+
+
+if __name__ == "__main__":
+ main()
diff --git a/atomic-examples/phoenix-tracing/phoenix_tracing/tools/calculator.py b/atomic-examples/phoenix-tracing/phoenix_tracing/tools/calculator.py
new file mode 100644
index 0000000..bc21dd3
--- /dev/null
+++ b/atomic-examples/phoenix-tracing/phoenix_tracing/tools/calculator.py
@@ -0,0 +1,91 @@
+from pydantic import Field
+from sympy import sympify
+
+from atomic_agents import BaseIOSchema, BaseTool, BaseToolConfig
+
+
+################
+# INPUT SCHEMA #
+################
+class CalculatorToolInputSchema(BaseIOSchema):
+ """
+ Tool for performing calculations. Supports basic arithmetic operations
+ like addition, subtraction, multiplication, and division, as well as more
+ complex operations like exponentiation and trigonometric functions.
+ Use this tool to evaluate mathematical expressions.
+ """
+
+ expression: str = Field(..., description="Mathematical expression to evaluate. For example, '2 + 2'.")
+
+
+#################
+# OUTPUT SCHEMA #
+#################
+class CalculatorToolOutputSchema(BaseIOSchema):
+ """
+ Schema for the output of the CalculatorTool.
+ """
+
+ result: str = Field(..., description="Result of the calculation.")
+
+
+#################
+# CONFIGURATION #
+#################
+class CalculatorToolConfig(BaseToolConfig):
+ """
+ Configuration for the CalculatorTool.
+ """
+
+ pass
+
+
+#####################
+# MAIN TOOL & LOGIC #
+#####################
+class CalculatorTool(BaseTool[CalculatorToolInputSchema, CalculatorToolOutputSchema]):
+ """
+ Tool for performing calculations based on the provided mathematical expression.
+
+ Attributes:
+ input_schema (CalculatorToolInputSchema): The schema for the input data.
+ output_schema (CalculatorToolOutputSchema): The schema for the output data.
+ """
+
+ input_schema = CalculatorToolInputSchema
+ output_schema = CalculatorToolOutputSchema
+
+ def __init__(self, config: CalculatorToolConfig = CalculatorToolConfig()):
+ """
+ Initializes the CalculatorTool.
+
+ Args:
+ config (CalculatorToolConfig): Configuration for the tool.
+ """
+ super().__init__(config)
+
+ def run(self, params: CalculatorToolInputSchema) -> CalculatorToolOutputSchema:
+ """
+ Executes the CalculatorTool with the given parameters.
+
+ Args:
+ params (CalculatorToolInputSchema): The input parameters for the tool.
+
+ Returns:
+ CalculatorToolOutputSchema: The result of the calculation.
+ """
+ # Convert the expression string to a symbolic expression
+ parsed_expr = sympify(str(params.expression))
+
+ # Evaluate the expression numerically
+ result = parsed_expr.evalf()
+ return CalculatorToolOutputSchema(result=str(result))
+
+
+#################
+# EXAMPLE USAGE #
+#################
+if __name__ == "__main__":
+ calculator = CalculatorTool()
+ result = calculator.run(CalculatorToolInputSchema(expression="sin(pi/2) + cos(pi/4)"))
+ print(result) # Expected output: {"result":"1.70710678118655"}
diff --git a/atomic-examples/phoenix-tracing/pyproject.toml b/atomic-examples/phoenix-tracing/pyproject.toml
new file mode 100644
index 0000000..70511c0
--- /dev/null
+++ b/atomic-examples/phoenix-tracing/pyproject.toml
@@ -0,0 +1,28 @@
+[build-system]
+requires = ["hatchling"]
+build-backend = "hatchling.build"
+
+[tool.hatch.build.targets.wheel]
+packages = ["phoenix_tracing"]
+
+[project]
+name = "phoenix-tracing"
+version = "1.0.0"
+description = "Trace Atomic Agents agents and tools in Arize Phoenix with OpenInference"
+readme = "README.md"
+authors = [
+ { name = "Kenny Vaneetvelde", email = "[email protected]" }
+]
+requires-python = ">=3.12"
+dependencies = [
+ "atomic-agents[observability]",
+ "arize-phoenix-otel>=0.17.0,<1.0.0",
+ "openinference-instrumentation-openai>=0.1.30,<0.2.0",
+ "instructor==1.14.5",
+ "openai>=2.0.0,<3.0.0",
+ "python-dotenv>=1.0.1,<2.0.0",
+ "sympy>=1.13.3,<2.0.0",
+]
+
+[tool.uv.sources]
+atomic-agents = { workspace = true }
diff --git a/docs/examples/index.md b/docs/examples/index.md
index 3bba10c..2b4e281 100644
--- a/docs/examples/index.md
+++ b/docs/examples/index.md
@@ -33,6 +33,17 @@ Comprehensive monitoring and error handling with the AtomicAgent hook system:
📂 **[View on GitHub](https://github.com/eigenwise/atomic-agents/tree/main/atomic-examples/hooks-example)** - Browse the complete source code and run the examples
+## Phoenix Tracing
+
+Trace agents and tools in Arize Phoenix with the optional OpenInference instrumentor:
+
+- Automatic AGENT and TOOL spans via `phoenix.otel.register(auto_instrument=True)`
+- LLM spans from the OpenAI instrumentor nested under each agent run
+- A parent chain span that ties a planner agent, a calculator tool and an answer agent into one trace
+- Session grouping with `using_session`
+
+📂 **[View on GitHub](https://github.com/eigenwise/atomic-agents/tree/main/atomic-examples/phoenix-tracing)** - Browse the complete source code and run the examples
+
## Basic Multimodal
Examples of working with images and text:
diff --git a/uv.lock b/uv.lock
index 9f54c63..5b7ea5f 100644
--- a/uv.lock
+++ b/uv.lock
@@ -29,6 +29,7 @@ members = [
"orchestration-agent",
"pdf-reader",
"persistent-memory",
+ "phoenix-tracing",
"progressive-disclosure",
"quickstart",
"rag-chatbot",
@@ -191,6 +192,25 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/15/b3/9b1a8074496371342ec1e796a96f99c82c945a339cd81a8e73de28b4cf9e/anyio-4.11.0-py3-none-any.whl", hash = "sha256:0287e96f4d26d4149305414d4e3bc32f0dcd0862365a4bddea19d7a1ec38c4fc", size = 109097, upload-time = "2025-09-23T09:19:10.601Z" },
]
+[[package]]
+name = "arize-phoenix-otel"
+version = "0.17.1"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "openinference-instrumentation" },
+ { name = "openinference-semantic-conventions" },
+ { name = "opentelemetry-exporter-otlp" },
+ { name = "opentelemetry-proto" },
+ { name = "opentelemetry-sdk" },
+ { name = "opentelemetry-semantic-conventions" },
+ { name = "typing-extensions" },
+ { name = "wrapt" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/35/c8/665028fbbb1638f01a1d0e6c7738a44e95c826e4784d2411a36f9c08e437/arize_phoenix_otel-0.17.1.tar.gz", hash = "sha256:63baec1128a036895dd49fa50222b9deac2cdeababbcca3d0f1f9b96e615fd6b", size = 28498, upload-time = "2026-08-10T17:36:29.629Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/d2/16/6027af2a69a5a150f7e2242cdc96127c912755b86c1395752582e96572bf/arize_phoenix_otel-0.17.1-py3-none-any.whl", hash = "sha256:ef2468290e73d3149729c72715267e547f5ad9a6700331dd2ca53b1a192d2a24", size = 22334, upload-time = "2026-08-10T17:36:28.038Z" },
+]
+
[[package]]
name = "arxiv-search"
version = "1.0.0"
@@ -2744,6 +2764,25 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/7c/8c/330101bedf614dc937d8690462ed0ad4efd7f6c3248a76a5484ba6f885e3/openinference_instrumentation-0.1.71-py3-none-any.whl", hash = "sha256:07833fcf5d493a2ed15da16a0af70d6127d39f18ffa68f4d6827107e49fc6753", size = 54511, upload-time = "2026-10-07T20:26:39.634Z" },
]
+[[package]]
+name = "openinference-instrumentation-openai"
+version = "0.1.65"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "anyio" },
+ { name = "openinference-instrumentation" },
+ { name = "openinference-semantic-conventions" },
+ { name = "opentelemetry-api" },
+ { name = "opentelemetry-instrumentation" },
+ { name = "opentelemetry-semantic-conventions" },
+ { name = "typing-extensions" },
+ { name = "wrapt" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/bc/83/cb4fa924d602ddf47e495cfb776cb3c697955ca5d0363ed3dce565701bab/openinference_instrumentation_openai-0.1.65.tar.gz", hash = "sha256:86a83a656277165cc012c42e7bbc0569b7cf97b50081e11bd50cbd41f5c632de", size = 29859, upload-time = "2026-10-09T18:29:06.725Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/10/ce/003b9366e7f01eb0b8373156a31c92c71ef44289d27e963e09ca7c581686/openinference_instrumentation_openai-0.1.65-py3-none-any.whl", hash = "sha256:aa29ae89aecfed1a705b3b91c8d631fc7cd2f136b31eafb3229d9cf464bf83f0", size = 37952, upload-time = "2026-10-09T18:29:05.017Z" },
+]
+
[[package]]
name = "openinference-semantic-conventions"
version = "0.1.41"
@@ -2766,6 +2805,19 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/ae/a2/d86e01c28300bd41bab8f18afd613676e2bd63515417b77636fc1add426f/opentelemetry_api-1.38.0-py3-none-any.whl", hash = "sha256:2891b0197f47124454ab9f0cf58f3be33faca394457ac3e09daba13ff50aa582", size = 65947, upload-time = "2025-10-16T08:35:30.23Z" },
]
+[[package]]
+name = "opentelemetry-exporter-otlp"
+version = "1.38.0"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "opentelemetry-exporter-otlp-proto-grpc" },
+ { name = "opentelemetry-exporter-otlp-proto-http" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/c2/2d/16e3487ddde2dee702bd746dd41950a8789b846d22a1c7e64824aac5ebea/opentelemetry_exporter_otlp-1.38.0.tar.gz", hash = "sha256:2f55acdd475e4136117eff20fbf1b9488b1b0b665ab64407516e1ac06f9c3f9d", size = 6147, upload-time = "2025-10-16T08:35:52.53Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/fd/8a/81cd252b16b7d95ec1147982b6af81c7932d23918b4c3b15372531242ddd/opentelemetry_exporter_otlp-1.38.0-py3-none-any.whl", hash = "sha256:bc6562cef229fac8887ed7109fc5abc52315f39d9c03fd487bb8b4ef8fbbc231", size = 7018, upload-time = "2025-10-16T08:35:32.995Z" },
+]
+
[[package]]
name = "opentelemetry-exporter-otlp-proto-common"
version = "1.38.0"
@@ -2796,6 +2848,39 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/28/f0/bd831afbdba74ca2ce3982142a2fad707f8c487e8a3b6fef01f1d5945d1b/opentelemetry_exporter_otlp_proto_grpc-1.38.0-py3-none-any.whl", hash = "sha256:7c49fd9b4bd0dbe9ba13d91f764c2d20b0025649a6e4ac35792fb8d84d764bc7", size = 19695, upload-time = "2025-10-16T08:35:35.053Z" },
]
+[[package]]
+name = "opentelemetry-exporter-otlp-proto-http"
+version = "1.38.0"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "googleapis-common-protos" },
+ { name = "opentelemetry-api" },
+ { name = "opentelemetry-exporter-otlp-proto-common" },
+ { name = "opentelemetry-proto" },
+ { name = "opentelemetry-sdk" },
+ { name = "requests" },
+ { name = "typing-extensions" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/81/0a/debcdfb029fbd1ccd1563f7c287b89a6f7bef3b2902ade56797bfd020854/opentelemetry_exporter_otlp_proto_http-1.38.0.tar.gz", hash = "sha256:f16bd44baf15cbe07633c5112ffc68229d0edbeac7b37610be0b2def4e21e90b", size = 17282, upload-time = "2025-10-16T08:35:54.422Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/e5/77/154004c99fb9f291f74aa0822a2f5bbf565a72d8126b3a1b63ed8e5f83c7/opentelemetry_exporter_otlp_proto_http-1.38.0-py3-none-any.whl", hash = "sha256:84b937305edfc563f08ec69b9cb2298be8188371217e867c1854d77198d0825b", size = 19579, upload-time = "2025-10-16T08:35:36.269Z" },
+]
+
+[[package]]
+name = "opentelemetry-instrumentation"
+version = "0.59b0"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "opentelemetry-api" },
+ { name = "opentelemetry-semantic-conventions" },
+ { name = "packaging" },
+ { name = "wrapt" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/04/ed/9c65cd209407fd807fa05be03ee30f159bdac8d59e7ea16a8fe5a1601222/opentelemetry_instrumentation-0.59b0.tar.gz", hash = "sha256:6010f0faaacdaf7c4dff8aac84e226d23437b331dcda7e70367f6d73a7db1adc", size = 31544, upload-time = "2025-10-16T08:39:31.959Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/10/f5/7a40ff3f62bfe715dad2f633d7f1174ba1a7dd74254c15b2558b3401262a/opentelemetry_instrumentation-0.59b0-py3-none-any.whl", hash = "sha256:44082cc8fe56b0186e87ee8f7c17c327c4c2ce93bdbe86496e600985d74368ee", size = 33020, upload-time = "2025-10-16T08:38:31.463Z" },
+]
+
[[package]]
name = "opentelemetry-proto"
version = "1.38.0"
@@ -3059,6 +3144,31 @@ requires-dist = [
{ name = "python-dotenv", specifier = ">=1.0.1,<2.0.0" },
]
+[[package]]
+name = "phoenix-tracing"
+version = "1.0.0"
+source = { editable = "atomic-examples/phoenix-tracing" }
+dependencies = [
+ { name = "arize-phoenix-otel" },
+ { name = "atomic-agents", extra = ["observability"] },
+ { name = "instructor" },
+ { name = "openai" },
+ { name = "openinference-instrumentation-openai" },
+ { name = "python-dotenv" },
+ { name = "sympy" },
+]
+
+[package.metadata]
+requires-dist = [
+ { name = "arize-phoenix-otel", specifier = ">=0.17.0,<1.0.0" },
+ { name = "atomic-agents", extras = ["observability"], editable = "." },
+ { name = "instructor", specifier = "==1.14.5" },
+ { name = "openai", specifier = ">=2.0.0,<3.0.0" },
+ { name = "openinference-instrumentation-openai", specifier = ">=0.1.30,<0.2.0" },
+ { name = "python-dotenv", specifier = ">=1.0.1,<2.0.0" },
+ { name = "sympy", specifier = ">=1.13.3,<2.0.0" },
+]
+
[[package]]
name = "pillow"
version = "12.0.0"