#35 AI agents and MCP 5.1 min
Trace Every Agent Step with OpenTelemetry in Python
Which step of my agent failed, and which was slow? One trace showed both, with the time each step took.
Make the slow step quick
Copy the code folder first. In agent.py change time.sleep(0.4) in work_out_refund to time.sleep(0.02). Predict which step the report names as slowest now, and roughly how long the agent.run line shows. The OpenTelemetry packages need to be installed as in the video to run it, so this is a prediction task.
A hint
The lookup step sleeps for 0.05 seconds, which is now longer than the refund step.
Forget to set the error status
In send_email in agent.py delete the span.set_status line but keep span.record_exception. Predict the failed steps line, the exception events line, and the status printed next to tool.send_email.
A hint
The video says a trace would look healthy if the failure were not recorded on the span. Compare the status and the failed steps line with the original run.
Add a fourth tool
In agent.py add a function archive(pounds) that opens a span named tool.archive and sleeps for 0.1 seconds, and call it in run() after send_email. Predict the number on the last report line, and where the new span appears in the printed list.
A hint
The report prints the parent first, then its children in the order they started.
Choose safe attributes
Look at the attributes set in agent.py: order.id, refund.pounds and email.sent. Write down two more details of a refund that you would add to a span, and one that you would never add. Use the mistakes list in the video to explain why.
A hint
The video says to put inputs that matter as attributes, and never secrets or personal data.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
The slow step and the failure are staged with a sleep and a raised error. No model call, no collector, no sampling and no network. One trace in one process, timed on Windows, where very short spans are only roughly timed.
Up next in AI agents and MCP
More in AI agents and MCP
- #01 Local AI Agent in Python: No API Key (Ollama)
- #12 Multi-Agent Patterns in Plain Python: Pipeline, Router, Loop
- #22 OpenAI Agents SDK vs LangGraph vs Plain Python
- #23 Agent Memory: What to Keep and What to Forget
- #24 Approval Gates for Risky Agent Actions
- #25 Tool Errors: Retries, Fallbacks and Made-Up Answers
Related videos
- #235 An End-to-End Audit Trail for AI Agent Actions in Python Enterprise architecture and AI agents
- #02 RAG From Scratch in Python: Chunk, Embed, Retrieve (Local) RAG and vector search
- #03 pgvector vs Chroma vs Qdrant: Same Data, Real Results RAG and vector search
- #05 Structured Outputs in Python: Valid JSON Is Not Correct JSON LLM engineering and evals
