Lesson Learned: LangSmith Tracing Integration
Lesson Learned: LangSmith Tracing Integration
ID: ll_050 Date: 2025-12-16 Severity: HIGH Category: Observability Impact: Enables full visibility into every trade decision for debugging and compliance
What Happened
The trading system was making decisions via the RAG/ML trade gate, but these decisions were not being traced to LangSmith. When the CEO checked LangSmith, there were no traces from the trading runs, making it impossible to debug or audit decisions.
Root Cause
- The LangSmith tracer module existed (
src/observability/langsmith_tracer.py) but was NOT wired into the trade gate - The
LANGSMITH_API_KEYwas not configured in GitHub Actions workflows - Initial integration attempt used context manager incorrectly (manual
__enter__/__exit__calls)
Solution
- Integrated tracing into mandatory trade gate (
src/safety/mandatory_trade_gate.py)- Every
validate_trade()call now creates a trace - Traces include: symbol, amount, side, strategy, RAG warnings, ML anomalies, decision
- Every
- Integrated tracing into trade execution (
src/execution/alpaca_executor.py)- Every executed order is traced
- Traces include: order details, price, commission, broker
- Added workflow configuration (
.github/workflows/daily-trading.yml)LANGSMITH_API_KEYandLANGCHAIN_API_KEYenvironment variablesLANGCHAIN_PROJECTset to âai-trading-systemâLANGCHAIN_TRACING_V2enabled
- Added verification script (
scripts/verify_trade_gate_tracing.py)- Run manually to confirm tracing is working
Prevention Rules
- ALWAYS trace critical decisions - Any decision that could lose money MUST be traced
- Use context managers correctly - Use
with tracer.trace() as span:not manual enter/exit - Check observability after deployment - Verify traces appear in LangSmith after each deploy
- Add API keys to workflows - Any new observability tool needs its secrets in GitHub Actions
Code Pattern
# CORRECT usage of LangSmith tracer
from src.observability.langsmith_tracer import TraceType, get_tracer
def my_decision_function():
if LANGSMITH_AVAILABLE:
tracer = get_tracer()
with tracer.trace(
name="my_decision",
trace_type=TraceType.DECISION,
) as span:
span.inputs = {"key": "value"}
result = do_work()
span.add_output("result", result)
span.add_metadata({"context": "info"})
return result
Files Modified
src/safety/mandatory_trade_gate.py- Added_trace_gate_decision()methodsrc/execution/alpaca_executor.py- Added_trace_trade_execution()method.github/workflows/daily-trading.yml- Added LangSmith environment variablesscripts/verify_trade_gate_tracing.py- New verification script
Action Required
You MUST add LANGSMITH_API_KEY to GitHub Secrets:
- Go to: Settings â Secrets â Actions
- Click âNew repository secretâ
- Name:
LANGSMITH_API_KEY - Value: Your key from https://smith.langchain.com