Structured Logging is the practice of emitting log records as machine-parseable structured data (typically JSON) rather than unstructured human-readable text — enabling powerful querying, aggregation, alerting, and analysis of AI system behavior, performance, and errors using SQL-like queries and dashboards rather than brittle string parsing and grep-based log hunting.
What Is Structured Logging?
- Definition: A logging approach where each log entry is a structured data object with defined fields (timestamp, level, message, `request_id`, `user_id`, model, `latency_ms`, `token_count`) rather than a free-form text string — making log data queryable like a database table.
- Contrast with Unstructured Logging:
- Unstructured: `[INFO 2024-01-15 10:32:15] Model predicted 'cat' with 0.92 confidence in 145ms`
- Structured: `{"timestamp": "2024-01-15T10:32:15Z", "level": "INFO", "event": "prediction", "class": "cat", "confidence": 0.92, "latency_ms": 145, "model_version": "v4.2", "request_id": "req_abc123"}`
- Queryable: Structured logs can be queried with SQL-like syntax — `SELECT AVG(latency_ms) FROM logs WHERE model_version = 'v4.2' AND confidence > 0.9` — without first extracting fields from unstructured text.
- Industry Standard: Modern observability platforms (Datadog, Splunk, Elasticsearch, CloudWatch Logs Insights) natively query structured JSON logs.
Why Structured Logging Matters for AI Systems
- Performance Analysis: Query `SELECT model, AVG(latency_ms) FROM logs GROUP BY model` to compare model performance across versions — using numeric latency and model fields.
- Error Diagnosis: Filter `WHERE error_type = 'rate_limit' AND retry_count > 3` to identify systematic retry failures — requires structured error fields.
- Cost Monitoring: Aggregate `SELECT user_id, SUM(input_tokens + output_tokens) FROM logs GROUP BY user_id` for per-user token usage accounting — requires token count fields in every log.
- Hallucination Tracking: Log fact-check results structurally — `{"event": "fact_check", "result": "failed", "claim": "...", "source_contradiction": "..."}` — then query failure rates over time.
- Alerting: Alert when the error fraction exceeds 0.05 or the 95th-percentile latency exceeds 5,000 ms for a model within a defined time window — requires numeric fields in structured log data.
- Audit Compliance: Reconstruct complete request histories for compliance audits by querying structured logs filtered by user_id, request_id, or date range.
Structured Logging Implementation
Python with structlog (integration sketch):
The application supplies `llm`, `count_tokens`, and `RateLimitError`; configure structlog processors to emit JSON and add timestamps and levels.
import structlog
from time import perf_counter
logger = structlog.get_logger()
def process_llm_request(request_id: str, user_id: str, query: str) -> str:
start_time = perf_counter()
try:
response = llm.generate(query)
duration_ms = (perf_counter() - start_time) * 1000
logger.info(
"llm_request_completed",
request_id=request_id,
user_id=user_id,
model="gpt-4o",
input_tokens=count_tokens(query),
output_tokens=count_tokens(response),
latency_ms=round(duration_ms),
success=True
)
return response
except RateLimitError as e:
logger.warning(
"llm_rate_limit",
request_id=request_id,
user_id=user_id,
retry_after=e.retry_after,
success=False
)
raiseOutput JSON:
{
"timestamp": "2024-01-15T10:32:15.234Z",
"level": "info",
"event": "llm_request_completed",
"request_id": "req_abc123",
"user_id": "usr_456",
"model": "gpt-4o",
"input_tokens": 342,
"output_tokens": 187,
"latency_ms": 1847,
"success": true
}Key Fields for AI System Logs
| Field | Type | Purpose |
|---|---|---|
| timestamp | ISO 8601 | Time correlation |
| `request_id` | String (optionally UUID) | Request tracing |
| `user_id` | String | Per-user analysis |
| `session_id` | String | Conversation tracking |
| event | String | Log type classification |
| model | String | Model version tracking |
| `input_tokens` | Integer | Cost accounting |
| `output_tokens` | Integer | Cost accounting |
| `latency_ms` | Integer | Performance monitoring |
| `retry_count` | Integer | Reliability tracking |
| `error_type` | String | Error classification |
| `rag_chunks_retrieved` | Integer | RAG performance |
| confidence | Float | Quality tracking |
| success | Boolean | Success rate monitoring |
Log Level Strategy for AI Systems
- DEBUG: Detailed diagnostics; omit sensitive payloads or use approved, redacted test data.
- INFO: Request completion with token counts, latency, model version.
- WARNING: Retries, rate limits, format corrections, low-confidence outputs.
- ERROR: Failed requests after max retries, validation failures, unexpected exceptions.
- CRITICAL: Service-wide failures, circuit breaker trips, data loss events.
Structured Log Querying Examples
Run each query separately in CloudWatch Logs Insights:
# Average latency by model
fields @timestamp, model, latency_ms
| filter event = "llm_request_completed"
| stats avg(latency_ms) as avg_latency by model
| sort avg_latency desc# Failed request count by hour (not an error rate)
filter success = false
| stats count() as errors by bin(1h)# Token volume by user (top 10; pricing is not included)
filter event = "llm_request_completed"
| stats sum(input_tokens + output_tokens) as total_tokens by user_id
| sort total_tokens desc
| limit 10PII Handling in Logs
AI system logs must handle personally identifiable information carefully:
- Never log raw user query content in production without PII scrubbing.
- Log query metadata (length, topic classification) rather than content.
- Apply field-level encryption or masking for sensitive structured fields.
- Apply the organization’s retention and deletion policies to logs and their backups.
- Use separate log streams for high-sensitivity data with stricter access controls.
Structured logging is the observability foundation that transforms AI systems from black boxes into monitorable, debuggable, and auditable production infrastructure — by emitting machine-parseable structured data from every significant operation, teams gain the ability to answer operational questions — why did that request fail, which model version is slower, which users are approaching token limits — with queries rather than grep, enabling data-driven AI operations at scale.
References: [structlog configuration](https://structlog.org/en/stable/standard-library.html) and [CloudWatch query syntax](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html).