Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

docs: document record_stream and trace convert

xf gong81fa4d1131f5
diff --git a/skills/cubepi-trace/SKILL.md b/skills/cubepi-trace/SKILL.md
index cfdb3a1..e48b7cc 100644
--- a/skills/cubepi-trace/SKILL.md
+++ b/skills/cubepi-trace/SKILL.md
@@ -120,6 +120,56 @@ uv run cubepi trace stats --by model --meta user_id=usr_9
uv run cubepi trace ls --show-meta conversation_id,user_id
```
+## Replaying a failing LLM call (`trace convert`)
+
+When the span tree + content aren't enough and you need to replay the exact API
+call — same messages, same tools, same parameters — use `trace convert` to
+reconstruct the request body from a recorded `chat` span (requires
+`record_content=True`):
+
+```bash
+# Reconstruct the last LLM call as an OpenAI JSON body
+uv run cubepi trace convert <run_id>
+
+# Pick a specific call by span_id prefix (copy [0x…] from `view`)
+uv run cubepi trace convert <run_id> --span 0xbb7eb1
+
+# Shell-executable curl (uses $BASE_URL / $API_KEY env vars)
+uv run cubepi trace convert <run_id> --span 0xbb7eb1 --format curl
+
+# Anthropic Messages API shape
+uv run cubepi trace convert <run_id> --format anthropic
+```
+
+The `[0x…]` suffix from each `chat` node in `view` output is the span_id — paste
+it directly as `--span 0x<prefix>`. No need to count turns.
+
+## Debugging streaming failures (`record_stream`)
+
+For issues where the span tree doesn't show the problem — missing tool call
+arguments, duplicate tool executions, truncated output — enable stream recording
+in the tracer:
+
+```python
+Tracer(record_content=True, record_stream=True, stream_dir="./cubepi-traces", …)
+```
+
+This writes `<stream_dir>/<run_id>.stream.jsonl` — one JSON line per raw
+`StreamEvent`. Check the file after a failing run:
+
+```bash
+# See all toolcall events for a run
+grep '"type": "toolcall' cubepi-traces/<date>/<run_id>.stream.jsonl | python -m json.tool
+
+# Check for duplicate toolcall_end (double finish_reason bug pattern):
+grep '"toolcall_end"' cubepi-traces/<date>/<run_id>.stream.jsonl | wc -l # expect 1
+```
+
+Key fields: `t` (elapsed seconds), `type`, `ci` (content index), `accumulated`
+(running arg chars), `args_chars` (final count at end). An `args_chars: 0` at
+`toolcall_end` means no argument chunks ever arrived — the model sent an empty
+tool call.
+
If the CLI view still isn't enough, the files are plain JSONL — one span per
line — so you can parse them directly (e.g. with `python -c`/`jq`) to pull a
specific attribute. Useful attribute keys:
diff --git a/website/docs/guides/tracing/cli.md b/website/docs/guides/tracing/cli.md
index a6dcb8b..df9cd7e 100644
--- a/website/docs/guides/tracing/cli.md
+++ b/website/docs/guides/tracing/cli.md
@@ -123,6 +123,39 @@ cubepi trace stats --by model --meta user_id=usr_9
cubepi trace stats --by tool --meta conversation_id=conv_123
```
+## `convert` — reconstruct an API request body
+
+When you need to replay a specific LLM call — reproduce a failure, test a prompt
+change, or run a raw `curl` against the same context — `convert` reads a recorded
+`chat` span and outputs the complete request body.
+
+Requires `record_content=True`.
+
+```bash
+# Default: last chat span in the trace, OpenAI JSON format
+cubepi trace convert <trace_id>
+
+# Select which LLM call to reconstruct
+cubepi trace convert <trace_id> --turn 2 # 2nd chat span (1-indexed)
+cubepi trace convert <trace_id> --span 0xbb7eb1 # by span_id prefix (from `view`)
+
+# Output formats
+cubepi trace convert <trace_id> --format openai # default — JSON request body
+cubepi trace convert <trace_id> --format anthropic # Anthropic Messages API body
+cubepi trace convert <trace_id> --format curl # shell-executable curl command
+```
+
+The `[0x…]` span id from `view` output goes directly into `--span`:
+
+```
+├── chat kimi-k2.6 31704.5ms [0xbb7eb192] ← paste as: --span 0xbb7eb1
+├── chat kimi-k2.6 32420.2ms [0x7c76f48d] ← or: --span 0x7c76f4
+```
+
+The reconstructed body includes the full conversation history, the system prompt,
+all tool definitions, and request parameters (`model`, `max_tokens`,
+`temperature`). Pipe to `python -m json.tool`, `jq`, or directly to a replay script.
+
## Beyond the CLI
The files are plain JSONL — one span per line — so you can parse them directly
diff --git a/website/docs/guides/tracing/content-recording.md b/website/docs/guides/tracing/content-recording.md
index 9a2674f..a469c95 100644
--- a/website/docs/guides/tracing/content-recording.md
+++ b/website/docs/guides/tracing/content-recording.md
@@ -112,6 +112,41 @@ the raw provider response on every chat span, multi-turn agentic runs can
get large fast. Drop or summarise via `redact` for any field over your
budget.
+## Stream-level recording
+
+`record_content` captures the final assembled request and response on each `chat`
+span, but not the individual streaming chunks. For post-mortem debugging of
+streaming failures — empty tool call arguments, duplicate events, truncated
+output — enable `record_stream` to write a chunk-by-chunk event log alongside the
+main trace:
+
+```python
+tracer = Tracer(
+ record_content=True, # needed for trace convert
+ record_stream=True, # ← per-chunk event log
+ stream_dir="./cubepi-traces", # where to write <run_id>.stream.jsonl
+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],
+)
+```
+
+`record_stream` writes `<stream_dir>/<run_id>.stream.jsonl` (one JSON line per
+`StreamEvent`). Every line carries `t` (elapsed seconds from run start) and
+`type`. Tool-call lines also include `ci` (content index), `id`, `name`, delta
+sizes, and argument previews:
+
+```json
+{"t": 5.873, "type": "toolcall_start", "ci": 1, "id": "toolu_...", "name": "show_widget"}
+{"t": 5.875, "type": "toolcall_delta", "ci": 1, "chars": 11, "accumulated": 11, "preview": "{\"title\": \""}
+{"t": 33.177, "type": "toolcall_end", "ci": 1, "id": "toolu_...", "args_chars": 7465, "args_preview": "{\"title\": \"CubePi..."}
+```
+
+This makes it straightforward to confirm whether argument chunks ever arrived, or
+whether the same event fired twice (e.g. a provider that sends `finish_reason`
+twice produces two `toolcall_end` lines for the same `ci`).
+
+`record_stream` is independent of `record_content` — turn it on only in debugging
+sessions. Files can grow large for long-running, tool-heavy agents.
+
## Auditing what's recorded
The recorder always sets `service.name`, `gen_ai.agent.name`, and