feat(agents): opt-in capture of raw LLM request/response bodies - #3843
Conversation
tracing_http_client wraps the OpenAI wire so every provider call lands whole under <run log dir>/llm as <seq>-request/-response.json pairs; McpClient wires it into init_chat_model and exposes trace_dir() over RPC so harnesses can link agent steps to the exact payloads.
Tracing now happens only when McpClientConfig.trace_dir is set explicitly; a plain dimos run no longer taps the wire or writes under <run log dir>/llm. Harnesses that want capture pass trace_dir.
isinstance(model, ChatOpenAI) on the init_chat_model result replaces _attempt_infer_model_provider; a langchain bump can no longer break McpClient at import time. Traced set unchanged.
write_normalized and latest_pair have no callers here; they land with the eval framework that consumes them. This module is now just the tap.
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## main #3843 +/- ##
==========================================
+ Coverage 77.58% 77.60% +0.01%
==========================================
Files 1315 1317 +2
Lines 124697 124794 +97
Branches 10867 10868 +1
==========================================
+ Hits 96748 96841 +93
- Misses 24816 24820 +4
Partials 3133 3133
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
Greptile SummaryThis change adds opt-in raw OpenAI request/response tracing and exposes the trace directory through MCP client configuration. A concurrent shared trace directory can lose or misassociate records: independently created tracing clients can select the same sequence number and overwrite a request/response pair. The non-OpenAI initialization path was also exercised; although it temporarily constructs an unused tracing client, that client is immediately collected before opening connections, so it does not produce the reported connection-pool leak. The change is not safe to merge until trace sequence allocation is made atomic for all clients and processes writing to the same directory. Confidence Score: 4/5Not safe to merge while concurrent writers can overwrite trace pairs in a shared trace directory. Focused executable checks reproduced the shared-directory overwrite using the real tracing hooks and disproved the reported retained-client leak on the non-OpenAI path. Files Needing Attention: dimos/agents/llm_trace.py needs directory-scoped, cross-process-safe trace sequence reservation. dimos/agents/mcp/mcp_client.py was exercised and needs no change for the reported client-lifetime concern.
What T-Rex did
|
bed7ea3 to
22cf720
Compare
Codecov failed PR 3843 on patch coverage: llm_trace.py landed at 38.77% with the hook bodies untested. Exercise tracing_http_client over an httpx.MockTransport: record pair written with auth header dropped, non-JSON bodies kept as text, sequence numbers increasing.
Contribution path
Problem
We need to save agent trajectories of our MCP client but it at present has no way of saving that information.
Solution
An opt-in process that instruments the httpx client for OpenAI requests. The default behavior does not affect any existing hot paths. The first consumer will be the eval framework: #3774
Auth headers are not saved.
Langchain does not have a provider-agnostic hook for capturing raw requests/responses, so it would require more tinkering for other providers. Because we just use OpenAI, I kept it simple. Maybe we'll be off Langchain by the time we need universal coverage :)
How to Test
AI assistance
Fable 5 wrote 99.9% of this. I reviewed every line.
Checklist