CLAUDE LABJP
FORK — Claude Code 2.1.212 changes what /fork does: it copies your conversation into a new background session with its own row in claude agents, so you can keep working. The old in-session subagent is now /subtaskLIMITS — WebSearch calls are now capped at 200 per session by default, and subagent spawns get the same 200 ceiling, so a runaway search or delegation loop stops on its ownMCPBG — MCP tool calls running past two minutes now move to the background automatically, keeping the session usable. Tune the threshold with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSPLANFIX — Fixed plan mode auto-running file-modifying Bash commands such as touch and rm without a permission prompt or an SDK canUseTool callbackSONNET5 — Claude Sonnet 5 is running on introductory pricing of $2 per million input tokens and $10 per million output. After August 31 it moves to $3 and $15IPO — Bankers are reportedly lining up investor meetings for Anthropic ahead of a possible public listing as soon as OctoberFORK — Claude Code 2.1.212 changes what /fork does: it copies your conversation into a new background session with its own row in claude agents, so you can keep working. The old in-session subagent is now /subtaskLIMITS — WebSearch calls are now capped at 200 per session by default, and subagent spawns get the same 200 ceiling, so a runaway search or delegation loop stops on its ownMCPBG — MCP tool calls running past two minutes now move to the background automatically, keeping the session usable. Tune the threshold with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSPLANFIX — Fixed plan mode auto-running file-modifying Bash commands such as touch and rm without a permission prompt or an SDK canUseTool callbackSONNET5 — Claude Sonnet 5 is running on introductory pricing of $2 per million input tokens and $10 per million output. After August 31 it moves to $3 and $15IPO — Bankers are reportedly lining up investor meetings for Anthropic ahead of a possible public listing as soon as October
Articles/API & SDK
API & SDK/2026-05-01Advanced

Claude API Telemetry on ClickHouse: A Production Guide to Cost, Latency, and Error Analytics

Stream per-request Claude API telemetry into ClickHouse, build sub-second dashboards with materialized views, and detect cost spikes, retry loops, and silent failures with practical SQL recipes.

claude-api81clickhouseobservability20analytics3production111

Premium Article

About six months after putting the Claude API into production, I opened a monthly invoice and felt my hands tremble. Costs had ballooned to 4.2x the previous month. Tracing the root cause took two full days. If I had per-request telemetry on the application side back then, I could have found the offending endpoint in roughly thirty minutes.

This article shows how to push per-request Claude API usage, cost, and latency data into ClickHouse, build the kind of fast aggregation and anomaly detection that Postgres struggles with, and run all of it at production quality. We will cover safe ingestion from Cloudflare Workers into ClickHouse Cloud, materialized views that return dashboards in under one second, and practical SQL recipes for catching the silent failures that never show up on an Anthropic invoice — retry loops, hallucination outliers, and latency regressions.

Why ClickHouse — How It Differs From Postgres, BigQuery, and Snowflake

Most teams start by writing telemetry to Postgres. I did the same thing. One row per request inserted into an events table, then aggregate with SQL. It looks simple enough.

The cracks appear once API traffic crosses roughly one million calls per day. Aggregations like SELECT model, sum(input_tokens), sum(output_tokens) FROM events WHERE ts > now() - interval '1 day' GROUP BY model start taking thirty seconds or more, even with carefully chosen indexes. Postgres is optimized for OLTP — row-oriented transactional access — and that orientation works against you when you need columnar aggregation.

The strengths of ClickHouse that matter most for telemetry, in priority order: columnar storage that returns SUM, AVG, and quantile() over hundreds of millions of rows in single-digit seconds; specialized engines like ReplacingMergeTree and AggregatingMergeTree that let you declare deduplication and pre-aggregation in DDL; materialized views that act as live aggregation tables — the foundation for sub-second dashboards; and high compression ratios — telemetry data has lots of repetition, and 10x compression is normal.

BigQuery and Snowflake are also columnar, but their slot startup latency (a few seconds to over ten) and per-query pricing make them awkward for the "frequently re-queried dashboard backend" use case. ClickHouse trades a richer SaaS feature set for sheer real-time responsiveness, which is exactly what you want for ops dashboards, alert evaluation, and Slack-bot reporting.

My personal heuristic, weighing team size, budget, and ops overhead: if you are running more than a million Claude API calls per day, ClickHouse is the first option I reach for. Below that volume, Postgres with a thoughtfully designed rollup table can still serve you well — pick to your scale.

What to Measure and the Minimal Schema That Actually Works

"Just put everything in" leads to schema bloat. Decide upfront on the minimum set of fields that will pay for themselves in production.

After running this schema across four products, here is what I recommend.

CREATE TABLE claude_api_events (
    -- Identifiers
    event_id UUID,
    request_id String,        -- Anthropic request-id header
    user_id String,           -- App-level user identifier
    workspace_id LowCardinality(String),  -- Multi-tenant support
 
    -- Time and model info
    ts DateTime64(3, 'UTC'),  -- Millisecond precision
    model LowCardinality(String),
    api_version LowCardinality(String),
 
    -- Request and response
    input_tokens UInt32,
    output_tokens UInt32,
    cache_read_tokens UInt32 DEFAULT 0,
    cache_creation_tokens UInt32 DEFAULT 0,
    stop_reason LowCardinality(String),
 
    -- Performance
    latency_ms UInt32,
    ttft_ms UInt32,           -- Time To First Token
    streaming UInt8,
 
    -- Outcome
    success UInt8,
    error_type LowCardinality(String) DEFAULT '',
    http_status UInt16,
 
    -- Application-layer context
    feature_id LowCardinality(String),  -- "summarize", "translate", etc.
    prompt_template_id LowCardinality(String),
    prompt_version UInt32 DEFAULT 0,
 
    -- Cost (precomputed so dashboard queries are cheap)
    cost_usd Decimal64(6) DEFAULT 0
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(ts)
ORDER BY (workspace_id, feature_id, ts)
TTL ts + INTERVAL 18 MONTH
SETTINGS index_granularity = 8192;

Three design decisions deserve emphasis.

First, lean on LowCardinality(String). Columns where the distinct value count is low — model name, feature_id — should be declared LowCardinality. Compression improves dramatically and GROUP BY runs faster. After switching, our storage footprint shrank by about 35 percent.

Second, your ORDER BY choice drives query performance. ClickHouse uses the leading prefix of ORDER BY as a primary key for partial scans. The order "tenant → feature → time" is tuned for the typical dashboard query: "the last 24 hours of feature X for tenant Y."

Third, precompute cost at insertion time. With cost_usd already populated, dashboard queries collapse into a single SUM. Pricing changes can be absorbed by materialized views, as we will see below.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Build the per-request cost tracking foundation that prevents the dreaded 'last month's API bill suddenly tripled' incident
Move from a Postgres telemetry table that chokes on aggregation queries to a ClickHouse + materialized view setup that returns dashboards in under one second
Learn concrete SQL patterns to detect hallucination outliers, retry storms, and latency regressions, so you catch incidents before users report them
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

API & SDK2026-06-18
When Your Claude API Response Cache Returns Stale Answers and Near-Miss Wrong Ones — Field Notes on Freshness and False-Hit Suppression
A Claude API response cache improves latency and cost immediately, but the problems that hurt in production are not average hit rate — they are stale hits and semantic false hits. Here is the key design, freshness management, false-hit suppression, and observability that keep a cache honest.
API & SDK2026-06-16
PII Masking for Claude API Lives or Dies on the Ledger — Restore, Encrypt, Measure
The hard part of masking PII before Claude API isn't detection — it's operating the token ledger you restore from. Encrypted storage, multi-instance sharing, and a daily leak-rate loop, with working code.
API & SDK2026-04-30
Building a Production Claude API Pipeline on Cloudflare Queues: Fault Tolerance, Backpressure, and Cost Control
A practical, code-first walkthrough for routing Claude API calls through Cloudflare Queues — covering producer/consumer code, retry-vs-DLQ branching, priority lanes, and token budgeting for production workloads.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →