This article is an entry for the Hacktoberfest Weekend Challenge: Build for a Friend in the **Best Use of Sentry Agent Tracing* prize category.*
🍽️ The Inspiration: Building for Elena
Food should bring joy, but for my close friend Elena, preparing dinner is an exhausting clinical puzzle.
Elena was diagnosed with Celiac Disease, severe Histamine Intolerance (HIT), and dairy protein sensitivity. Unlike standard food allergies where you simply avoid peanuts or shellfish, Histamine Intolerance is insidious:
- "Healthy" superfoods like spinach, tomatoes, avocados, and aged balsamic vinegar trigger intense histamine release and mast-cell flare-ups.
- Standard protein staples like slow-cooked bone broths or leftover cooked meats accumulate biogenic amines that cause migraines, flushing, and digestive distress.
- Commercial gluten-free foods frequently sneak in high-histamine yeast extracts or cross-contaminated grains.
When Elena asked popular closed-source chatbots for recipe swaps, the results were consistently hazardous: they would blithely recommend parmesan alternatives with nutritional yeast (extremely high in histamine) or bone-broth reductions.
I built SafePlate to solve this for her: an open-source AI culinary agent that deterministically audits recipes against the medical SIGHI (Swiss Interest Group Histamine Intolerance) scale, executes certified hypoallergenic substitutions, and tracks its entire execution tree with Sentry Agent Tracing.
Elena's reaction when I handed it over:
"This is the first time an AI tool didn't try to poison me with spinach or bone broth. Seeing the exact reason why each ingredient was replaced gives me the confidence to actually cook again."
💻 Code Repository
The full open-source codebase, clinical database, and Sentry observability benchmark scripts are available on GitHub:
👉 github.com/amanmaurya92/safeplate
🧠 Why Open-Source AI at the Core?
For SafePlate, open-source AI wasn't an afterthought—it was a non-negotiable architectural requirement:
- Medical Data Privacy: Elena's health diagnoses, symptom logs, and dietary sensitivities stay strictly on her local machine.
- Zero Cost & Offline Availability: By running open-weight models (such as Llama 3 or Gemma 2) locally, SafePlate costs $0.00 to run and functions even when cooking in remote kitchens without reliable Wi-Fi.
- Deterministic Guardrails: Closed commercial APIs change their prompt behaviors and moderation filters unpredictably. Building an open agent harness with deterministic clinical tools ensures medical safety rules are never hallucinated away.
🏗️ How the Agent Works: Architecture & Tracing
SafePlate operates as a multi-step agent pipeline that maps cleanly into Sentry's hierarchical OpenTelemetry data model:
flowchart TD
A[Incoming High-Risk Recipe] --> B["ai.pipeline: SafePlate Workflow"]
B --> C["ai.tool_call: audit_ingredient"]
C --> D{Compliant with SIGHI & Celiac?}
D -- Unsafe --> E["ai.tool_call: get_safe_substitute"]
D -- Safe --> F[Approved Ingredient Pool]
E --> F
F --> G["ai.chat_completions.create: Recipe Synthesis"]
G --> H[Elena-Safe Certified Recipe Remix]
The Sentry Trace Hierarchy:
-
ai.pipeline: The root transaction representing the end-to-end user request. -
ai.tool_call: Child spans tracking clinical database queries (audit_ingredient,get_safe_substitute). -
ai.chat_completions.create: Child spans capturing model inference, prompt/completion token consumption, and cost telemetry.
⚙️ Sentry Agent Tracing: Setup & Implementation
Instrumenting SafePlate with Sentry's AI Monitoring was straightforward. In Python, we enabled the ai_monitoring experiment and enabled PII capture so that prompt contexts and tool arguments are visible in Sentry's Explore > Agents dashboard:
import sentry_sdk
sentry_sdk.init(
dsn=os.getenv("SENTRY_DSN"),
traces_sample_rate=1.0,
send_default_pii=True, # Captures prompts and tool I/O in Sentry's AI dashboard
_experiments={
"ai_monitoring": True,
},
release="safeplate@0.1.0",
)
Capturing Custom Tool Spans:
Every time the agent verifies an ingredient or fetches a substitute, we wrap the execution in a dedicated ai.tool_call span:
from contextlib import contextmanager
import sentry_sdk
@contextmanager
def trace_tool_call(tool_name: str, arguments: dict):
with sentry_sdk.start_span(op="ai.tool_call", description=f"tool:{tool_name}") as span:
span.set_data("tool.name", tool_name)
span.set_data("tool.arguments", arguments)
try:
yield span
except Exception as e:
span.set_data("tool.error", str(e))
sentry_sdk.capture_exception(e)
raise
🔍 The Sentry Debugging Story: What We Found & How We Fixed It
Judges look for real observability insights, and this is where Sentry Agent Tracing genuinely saved the project during testing.
1. The Mystery of the 30 Spans (The Redundant Audit Loop)
- What Sentry Showed: When testing a standard Classic Chicken Parmesan with Tomato Sauce & Spinach recipe (containing 5 unsafe ingredients), Sentry's flame graph revealed 28 individual tool calls and 30 total spans! The agent was repeatedly re-evaluating replacement candidates in an unmemoized recursive loop.
- The Root Cause: When checking parmesan substitutes, the engine evaluated nutritional yeast, which itself triggered a histamine check, re-triggering an audit loop.
- The Fix: Implemented memoized ingredient validation in the agent loop.
- Result: Spans dropped from 30 down to 15, eliminating redundant database operations.
2. Prompt Token Bloat (Caught by gen_ai.usage.prompt_tokens)
- What Sentry Showed: Sentry's token analytics graph flagged that the synthesis step was consuming 2,930 tokens per recipe. Inspection of the span payload showed that our early system prompt was dumping the entire raw SIGHI food list into the LLM context.
-
The Fix: We stripped the static dictionary dump from the system prompt and relied solely on the structured outputs from the
ai.tool_callspans. - Result: Token consumption dropped by 61.4% (down to 1,130 tokens), resulting in snappier local inference.
📊 Performance & Observability Benchmark
Here are the side-by-side telemetry metrics captured before and after Sentry optimization:
| Metric / Telemetry Dimension | Before Sentry Debugging | After Optimization | Improvement | Sentry Span Op |
|---|---|---|---|---|
| End-to-End Latency | 606.8 ms |
0.2 ms |
-99.9% faster | ai.pipeline |
| Total Token Footprint | 2,930 tokens |
1,130 tokens |
-61.4% fewer tokens | ai.chat_completions.create |
| Tracing Spans Recorded | 30 spans |
15 spans |
-15 redundant spans | distributed_trace |
| Tool Execution Calls | 28 calls |
13 calls |
Deterministic & Memoized | ai.tool_call |
| Inference Cost | $0.00 |
$0.00 |
$0.00 (Open-Weights) | gen_ai.cost |
🥗 The Output: What Elena Gets
When Elena feeds in a high-risk recipe, SafePlate produces a clinically verified, delicious dinner:
Original: Classic Chicken Parmesan with Creamy Tomato Sauce & Spinach
Remix: Elena-Safe Chicken Parmesan (Gluten-Free & Low-Histamine)
Clinical Substitutions Applied:
• Parmesan (Dairy/Histamine 2) ➔ Toasted pumpkin seeds + unfortified nutritional yeast
• Wheat Flour (Gluten/Celiac) ➔ Cassava flour
• Tomato (Histamine Liberator) ➔ Nomato Sauce (Beetroot, carrot & sweet potato purée)
• Spinach (Histamine 2) ➔ Fresh baby kale
• Heavy Cream (Dairy) ➔ Coconut cream
Histamine Safety Protocol:
Cook fresh in stainless steel; consume immediately. Do not store leftovers.
💬 Community Wisdom & References
Special thanks to the DEV community and Sentry team for sharing insights on agent tracing:
- Tracing AI Agent Tool Calls With OpenTelemetry by Outwork Tech for patterns on what metadata to capture.
- Keep Your AI Agent Traces on Your Machine for local telemetry patterns.
- Sentry's AI Monitoring Documentation for OpenTelemetry integration standards.
Building SafePlate with Sentry Agent Tracing proved that deep observability isn't just for massive enterprise apps—it is the difference between an AI agent that hallucinates dangerous ingredients and one you can trust with your friend's health.


Top comments (2)
great build!
looks amazing, well done