observability.mdx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ---
  2. title: "Observability"
  3. description: "Track and analyze your agent performance and behavior by connecting with third party observability tools."
  4. icon: "eyes"
  5. ---
  6. Agency Swarm supports multiple observability approaches to help you track and analyze your agent's behavior and performance.
  7. ## Usage Payload
  8. When usage tracking is enabled, responses include a `usage` object with token counts and cost.
  9. ```json
  10. {
  11. "response": "Test response",
  12. "new_messages": [],
  13. "usage": {
  14. "request_count": 1,
  15. "cached_tokens": 0,
  16. "input_tokens": 10,
  17. "output_tokens": 20,
  18. "total_tokens": 30,
  19. "total_cost": 0.0
  20. }
  21. }
  22. ```
  23. **Fields:**
  24. - `request_count`: Number of model requests
  25. - `cached_tokens`: Tokens served from cache (if available)
  26. - `input_tokens`: Tokens sent to the model
  27. - `output_tokens`: Tokens generated by the model
  28. - `total_tokens`: Total tokens for the request
  29. - `total_cost`: Estimated cost (USD)
  30. - `reasoning_tokens` (optional): Reasoning tokens (if available)
  31. **Where you'll see it:**
  32. - [FastAPI Integration](/additional-features/fastapi-integration): `POST /get_response` returns JSON with `usage`, and the final `event: messages` in `POST /get_response_stream` includes `usage`.
  33. - [Running an Agency](/core-framework/agencies/running-agency#track-usage-and-cost): use `/cost` in the TUI to see session usage and cost.
  34. ## Supported Observability Platforms
  35. Agency Swarm supports three main observability approaches:
  36. <CardGroup cols={3}>
  37. <Card title="OpenAI Tracing" icon="chart-line" href="#openai-tracing">
  38. Built-in tracing using OpenAI's native tools
  39. </Card>
  40. <Card title="Langfuse" icon="gauge-high" href="#langfuse-tracing">
  41. Advanced tracing and debugging platform
  42. </Card>
  43. <Card title="AgentOps" icon="database" href="#agentops-tracing">
  44. Specialized agent monitoring and analytics
  45. </Card>
  46. </CardGroup>
  47. ## Getting Started
  48. Let's walk through setting up each tracing solution. You can use them individually or combine them for monitoring.
  49. <Tabs>
  50. <Tab title="OpenAI Tracing">
  51. <Steps>
  52. <Step title="Basic Setup">
  53. OpenAI tracing is built into Agency Swarm and requires no additional packages.
  54. </Step>
  55. <Step title="Implementation">
  56. ```python
  57. from agency_swarm import trace
  58. async def openai_tracing(input_message: str) -> str:
  59. agency_instance = create_agency()
  60. with trace("OpenAI tracing"):
  61. response = await agency_instance.get_response(message=input_message)
  62. return response.final_output
  63. ```
  64. </Step>
  65. <Step title="View Traces">
  66. After running your code, view your traces at [platform.openai.com/traces](https://platform.openai.com/traces)
  67. </Step>
  68. </Steps>
  69. </Tab>
  70. <Tab title="Langfuse">
  71. <Steps>
  72. <Step title="Install Package">
  73. ```bash
  74. pip install langfuse
  75. ```
  76. </Step>
  77. <Step title="Set Environment Variables">
  78. ```bash
  79. export LANGFUSE_SECRET_KEY=<your-secret-key>
  80. export LANGFUSE_PUBLIC_KEY=<your-public-key>
  81. export LANGFUSE_HOST=<your-host>
  82. ```
  83. </Step>
  84. <Step title="Implementation">
  85. ```python
  86. from langfuse import observe
  87. @observe()
  88. async def langfuse_tracing(input_message: str) -> str:
  89. agency_instance = create_agency()
  90. @observe()
  91. async def get_response_wrapper(message: str):
  92. return await agency_instance.get_response(message=message)
  93. response = await get_response_wrapper(input_message)
  94. return response.final_output
  95. ```
  96. </Step>
  97. <Step title="View Traces">
  98. Access your traces at [cloud.langfuse.com](https://cloud.langfuse.com) and select your project.
  99. </Step>
  100. </Steps>
  101. </Tab>
  102. <Tab title="AgentOps">
  103. <Steps>
  104. <Step title="Install Package">
  105. ```bash
  106. pip install agentops
  107. ```
  108. </Step>
  109. <Step title="Set Environment Variables">
  110. ```bash
  111. export AGENTOPS_API_KEY=<your-api-key>
  112. ```
  113. </Step>
  114. <Step title="Implementation">
  115. ```python
  116. import agentops
  117. async def agentops_tracing(input_message: str) -> str:
  118. agentops.init(
  119. auto_start_session=True,
  120. trace_name="Agentops tracing",
  121. tags=["openai", "agentops-example"]
  122. )
  123. tracer = agentops.start_trace(
  124. trace_name="Agentops tracing",
  125. tags=["openai", "agentops-example"]
  126. )
  127. agency_instance = create_agency()
  128. response = await agency_instance.get_response(message=input_message)
  129. agentops.end_trace(tracer, end_state="Success")
  130. return response.final_output
  131. ```
  132. </Step>
  133. <Step title="View Traces">
  134. When you run your code, AgentOps will print a session replay URL in the console that looks like: `https://app.agentops.ai/sessions?trace_id=<your-trace-id>`
  135. </Step>
  136. </Steps>
  137. </Tab>
  138. </Tabs>
  139. ## Implementation Example
  140. For a complete working example that demonstrates all three tracing methods with a multi-agent agency, see [observability.py](https://github.com/VRSEN/agency-swarm/blob/main/examples/observability.py) in the examples directory.
  141. The example shows:
  142. - How to set up a basic agency with CEO, Developer, and Data Analyst roles
  143. - Implementation of all three tracing methods (OpenAI, Langfuse, AgentOps)
  144. - A sample tool for data analysis
  145. - Error handling and proper tracing setup
  146. You can run the example with:
  147. ```bash
  148. python examples/observability_demo.py
  149. ```
  150. For more information about each platform's capabilities and configuration options, refer to their respective documentation:
  151. - [OpenAI Documentation](https://platform.openai.com/docs)
  152. - [Langfuse Documentation](https://langfuse.com/docs)
  153. - [AgentOps Documentation](https://docs.agentops.ai)