AI Application Log Design: From Console.log to Production Observability
Console.log in production is not a logging system — it is an observability gap
In early AI application development, console.log is almost everyone's first choice — fast, intuitive, no additional configuration. This works perfectly in local development environments, but once in production, it represents a systematic observability gap.
Console.log Is Not Production Logging
In Vercel, AWS Lambda, and other serverless environments, console.log output exists in process standard output, typically retained for only 24–72 hours and not structurally queryable or cross-period aggregatable. You need database records when:
- A user reports a conversation answer was wrong, but you cannot reconstruct the input and output at that time
- The billing statement shows an anomalous spike this month, but you cannot identify which feature or user group caused it
- The API repeatedly times out during a specific time window, but you don't know the frequency or pattern
Minimum Fields That Must Be Persisted
| Field | Purpose |
|---|---|
| request_id | Global unique ID for tracing cross-service request chains |
| user_id | Attribute cost and usage to specific users |
| model | Analyse actual usage distribution and cost share per model |
| input_tokens | Cost calculation basis |
| output_tokens | Cost calculation basis |
| cost_usd | Calculate at call time from token rates — don't wait for billing |
| latency_ms | End-to-end response time for performance monitoring |
| status | success / error / timeout for error rate tracking |
| error_code | Distinguish 429 rate limit, 500 server error, timeout |
| created_at | Standardised UTC+8 (Asia/Hong_Kong) storage |
Time Zone Standardisation — Often Overlooked
If some fields are stored in UTC and some in local time, cross-period query results will show hard-to-spot distortions — for example, "yesterday's peak period" statistics with the wrong window due to time zone inconsistency. Standardise all timestamps to UTC+8 at the database layer to avoid report calculation errors.
Meaningful Alerts Require Structured Logs
With clearly-fielded log records, meaningful alert rules become possible:
- Single user 24-hour cumulative cost exceeds threshold → alert administrator
- 429 error rate over past 5 minutes exceeds threshold → trigger rate limit alert
- P95 response time continuously exceeds N seconds → performance degradation alert
Threshold values should be stored as named constants, not hardcoded numbers scattered through business logic — otherwise every threshold adjustment requires a search-and-replace hunt.
Common Recording Omissions
Failed requests are often more analytically valuable than successful ones, yet they are the most commonly omitted. Another common issue: the cost field is not calculated at call time but reconstructed from API billing statements after the fact — this eliminates real-time cost control, and overspend is only discovered when the bill arrives at month end.
For more on AI production observability, see Silent Failure in AI Applications: Every Error Must Leave a Trace.
Summary
AI application logging is the observability infrastructure of the operation. Its design should be completed alongside the first version of the API call structure. The cost of retrofitting fields is far higher than getting it right at design time. The observability of token costs, model behaviour, and error patterns directly determines whether you can make an effective diagnosis when a problem appears — or are left guessing blindly.
Levi is a Hong Kong-based independent AI engineer building production AI logging and observability infrastructure. Contact for AI application log design consultation.
WhatsApp Free Initial Consultation → More enterprise case studies →Or email: support@hksoka.com