Back to KB
Difficulty
Intermediate
Read Time
9 min

Per-Step JSONL Logging for Agent Runs: Know What Your Agent Did and When

By Codcompass TeamΒ·Β·9 min read

Structured Agent Telemetry: Implementing Per-Step JSONL Audits in Python

Current Situation Analysis

Modern LLM agents operate as non-deterministic systems. When an agent executes a multi-step workflow involving tool calls, reasoning loops, and external API interactions, the internal state becomes opaque. In production environments, this opacity creates a critical vulnerability: when a user reports an incorrect response or a workflow fails, engineering teams often lack the data to reconstruct the execution path.

The industry pain point is the "black box" nature of agent runs. Without granular telemetry, debugging requires reproducing the exact stochastic conditions of the failure, which is frequently impossible. Developers are left guessing whether the issue stemmed from a hallucination, a tool failure, a context window limit, or an upstream data error.

This problem is frequently overlooked because teams prioritize agent capability over observability during development. Logging is often treated as an afterthought, resulting in unstructured console dumps or missing data entirely. However, structured step logging is not merely a debugging aid; it is a prerequisite for cost control, SLA enforcement, and safety auditing.

Data from production deployments indicates that structured JSONL logging introduces negligible overhead while providing maximum diagnostic value. A typical five-step agent run generates less than 2KB of log data. This efficiency makes it feasible to log every inference and tool interaction without impacting storage budgets or write latency, enabling comprehensive audit trails that were previously cost-prohibitive.

WOW Moment: Key Findings

The decision to implement per-step JSONL logging fundamentally shifts the operational profile of an agent system. The following comparison highlights why JSONL is the superior choice for agent telemetry compared to traditional logging strategies.

StrategyWrite LatencyStorage EfficiencyDebug GranularityOperational Overhead
No Logging0ms0 bytesNoneNone
Relational DB5–15ms per stepHigh overhead (indexes, rows)HighHigh (connection pooling, schema mgmt)
Unstructured Text<1msLowLow (regex parsing required)Medium (parsing complexity)
JSONL Step Log<0.5msLow (compact, append-only)High (structured, queryable)Low (file I/O only)

Why this matters: JSONL step logging provides the only approach that combines sub-millisecond write latency with high structural fidelity. This enables teams to capture every LLM turn and tool execution without introducing latency bottlenecks in the agent's critical path. The append-only nature ensures data integrity, while the line-delimited format allows for efficient streaming, filtering, and analysis using standard Unix tools or lightweight parsers. This finding enables production-grade observability that scales linearly with agent usage, regardless of throughput.

Core Solution

Implementing per-step JSONL telemetry requires a design that prioritizes atomicity, thread safety, and minimal overhead. The solution involves creating a telemetry class that manages an append-only file handle, maintains an in-memory buffer for summary calculations, and exposes methods for recording inference events and tool executions.

Architecture Decisions

  1. Append-Only JSONL: Each event is a single JSON object on a new line. This format is resilient to partial writes and allows for easy streaming consumption.
  2. Thread-Safe Writes: Agent loops often involve concurrent tool executions or background tasks. A lock ensures that file writes and in-memory updates are atomic, preventing corruption or race conditions.
  3. In-Memory Buffer: To support efficient summary generation without re-reading the file, events are cached in memory. This allows instant computation of token totals, error counts, and duration metrics.
  4. Compact Serialization: Using minimal JSON separators reduces file size, which is critical when logging

πŸŽ‰ Mid-Year Sale β€” Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register β€” Start Free Trial

7-day free trial Β· Cancel anytime Β· 30-day money-back