[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"blog-en-ai-pipeline-tracing-costs-mvp":3},{"id":4,"title":5,"author":6,"body":7,"cover":575,"description":576,"draft":577,"extension":578,"locale":579,"meta":580,"navigation":581,"path":582,"publishedAt":583,"seo":584,"seoTitle":585,"slug":586,"stem":587,"tags":588,"__hash__":590},"blog\u002Fen\u002Fblog\u002Fai-pipeline-tracing-costs-mvp.md","Minimal tracing and costs for AI pipelines in production","Manuel Tardivo",{"type":8,"value":9,"toc":564},"minimark",[10,27,52,57,60,92,96,103,228,235,244,260,264,274,295,302,306,309,330,336,340,343,365,376,380,457,464,471,542,546,560],[11,12,13,14,18,19,26],"p",{},"Before you buy an “LLM observability” suite, you can already tell whether a pipeline survives load and what it costs with ",[15,16,17],"strong",{},"a handful of numbers"," captured in your code or structured logs. This post outlines an MVP metric set aligned with ",[20,21,25],"a",{"href":22,"rel":23},"https:\u002F\u002Fwww.weave.works\u002Fblog\u002Fthe-red-method-patterns-for-architecting-microservices",[24],"nofollow","RED"," (rate, errors, duration), adapted to model calls.",[11,28,29,32,33,38,39,43,44,47,48,51],{},[15,30,31],{},"Verifiable premise",": per-token pricing changes—pull rates from your vendor’s ",[20,34,37],{"href":35,"rel":36},"https:\u002F\u002Fopenai.com\u002Fapi\u002Fpricing\u002F",[24],"pricing page"," (OpenAI, Anthropic, Google, etc.) and compute ",[40,41,42],"code",{},"estimated_cost"," with their documented formula (",[40,45,46],{},"input_tokens * price_in + output_tokens * price_out"," is typical). Here we focus on ",[15,49,50],{},"how to measure",", not fixed dollar amounts.",[53,54,56],"h2",{"id":55},"what-not-to-measure-at-first","What not to measure at first",[11,58,59],{},"Avoid these early anti-patterns:",[61,62,63,74,80,86],"ul",{},[64,65,66,69,70,73],"li",{},[15,67,68],{},"Thirty-chart dashboards"," before you can list a stable ",[40,71,72],{},"request_id"," linked to a minimal trace.",[64,75,76,79],{},[15,77,78],{},"“Approximate” tokens"," with no source—if the client hides usage, read the official response payload before inventing multipliers.",[64,81,82,85],{},[15,83,84],{},"Average latency only","—p95 tells you far more about UX and timeouts.",[64,87,88,91],{},[15,89,90],{},"Full prompt logging in prod"," without retention policy—storage cost + privacy risk.",[53,93,95],{"id":94},"mvp-metrics-definitions-and-units","MVP metrics: definitions and units",[11,97,98,99,102],{},"For each model ",[15,100,101],{},"invocation"," (HTTP call to the vendor or your gateway) record:",[104,105,106,122],"table",{},[107,108,109],"thead",{},[110,111,112,116,119],"tr",{},[113,114,115],"th",{},"Field",[113,117,118],{},"Units",[113,120,121],{},"Notes",[123,124,125,139,160,176,203,216],"tbody",{},[110,126,127,133,136],{},[128,129,130],"td",{},[40,131,132],{},"duration_ms",[128,134,135],{},"ms",[128,137,138],{},"Wall clock from send until full response (or stream end).",[110,140,141,150,153],{},[128,142,143,146,147],{},[40,144,145],{},"input_tokens"," \u002F ",[40,148,149],{},"output_tokens",[128,151,152],{},"ints",[128,154,155,156,159],{},"From the provider ",[40,157,158],{},"usage"," object when present.",[110,161,162,167,170],{},[128,163,164],{},[40,165,166],{},"ok",[128,168,169],{},"boolean",[128,171,172,175],{},[40,173,174],{},"false"," on non-2xx or exceptions before body completes.",[110,177,178,183,186],{},[128,179,180],{},[40,181,182],{},"error_class",[128,184,185],{},"short string",[128,187,188,189,192,193,192,196,192,199,202],{},"e.g. ",[40,190,191],{},"rate_limit",", ",[40,194,195],{},"timeout",[40,197,198],{},"invalid_request",[40,200,201],{},"provider_5xx",".",[110,204,205,210,213],{},[128,206,207],{},[40,208,209],{},"model",[128,211,212],{},"string",[128,214,215],{},"Exact model id sent to the API.",[110,217,218,223,225],{},[128,219,220],{},[40,221,222],{},"step",[128,224,212],{},[128,226,227],{},"If the pipeline has multiple stages (retrieve, classify, generate).",[11,229,230,231,234],{},"Use the same row shape for async jobs with ",[40,232,233],{},"job_id"," instead of synchronous HTTP.",[11,236,237,240,241,243],{},[15,238,239],{},"Percentiles",": compute p50 and p95 for ",[40,242,132],{}," at least daily (SQL or metrics backend). p95 surfaces slow tails and flaky vendors before the mean moves.",[11,245,246,249,250,253,254,256,257,259],{},[15,247,248],{},"Error rate",": ",[40,251,252],{},"errors \u002F attempts"," grouped by ",[40,255,209],{}," and ",[40,258,222],{},". A spike on one step usually means tool-calling or schema validation, not “the model in general”.",[53,261,263],{"id":262},"estimated-vs-measured-cost","Estimated vs measured cost",[11,265,266,269,270,273],{},[15,267,268],{},"Measured"," cost = actual tokens × list price. ",[15,271,272],{},"Estimated"," cost might be a worst-case budget per request (declared max tokens × worst price). When measured systematically undershoots or overshoots estimates, verify:",[275,276,277,283,289],"ol",{},[64,278,279,282],{},[15,280,281],{},"Caching"," or long stable prefixes changing real input tokens.",[64,284,285,288],{},[15,286,287],{},"Streaming"," truncations lowering output tokens.",[64,290,291,294],{},[15,292,293],{},"Hidden retries"," in the HTTP client multiplying billed calls.",[11,296,297,298,301],{},"Log ",[40,299,300],{},"attempt"," (1 = first try, 2 = retry) so one user action does not look like one request when it is three.",[53,303,305],{"id":304},"sensible-alerting","Sensible alerting",[11,307,308],{},"Three thresholds cover many teams:",[275,310,311,317,323],{},[64,312,313,316],{},[15,314,315],{},"Daily budget"," from summed tokens × price.",[64,318,319,322],{},[15,320,321],{},"p95 latency"," beyond a contractual SLA (e.g. 8s on a sync endpoint).",[64,324,325,327,328,202],{},[15,326,248],{}," > Y% for 15 minutes on the same ",[40,329,222],{},[11,331,332,333,335],{},"Avoid paging on a single 500—bucket with ",[40,334,182],{}," and volume.",[53,337,339],{"id":338},"when-to-adopt-a-dedicated-product","When to adopt a dedicated product",[11,341,342],{},"Move to Langfuse, Helicone, LangSmith, etc. when at least one is true:",[61,344,345,352,358],{},[64,346,347,348,351],{},"You need ",[15,349,350],{},"distributed tracing"," across services (DB retrieve + worker + LLM) with automatic correlation.",[64,353,354,357],{},[15,355,356],{},"Non-engineers"," must explore traces without raw log access.",[64,359,360,361,364],{},"You want ",[15,362,363],{},"versioned eval datasets"," co-located with traces.",[11,366,367,368,371,372,375],{},"Until then, a ",[40,369,370],{},"llm_calls"," table in Postgres\u002FSQLite with the columns above plus an index on ",[40,373,374],{},"created_at"," answers most “why did cost spike yesterday?” questions.",[53,377,379],{"id":378},"sample-sql","Sample SQL",[381,382,387],"pre",{"className":383,"code":384,"language":385,"meta":386,"style":386},"language-sql shiki shiki-themes github-light github-dark","SELECT\n  date_trunc('day', created_at) AS day,\n  model,\n  sum(input_tokens + output_tokens) AS tokens,\n  avg(duration_ms) AS avg_ms,\n  percentile_cont(0.95) WITHIN GROUP (ORDER BY duration_ms) AS p95_ms,\n  sum(CASE WHEN ok THEN 0 ELSE 1 END)::float \u002F count(*) AS error_rate\nFROM llm_calls\nWHERE created_at > now() - interval '7 days'\nGROUP BY 1, 2\nORDER BY 1 DESC, tokens DESC;\n","sql","",[40,388,389,397,403,409,415,421,427,433,439,445,451],{"__ignoreMap":386},[390,391,394],"span",{"class":392,"line":393},"line",1,[390,395,396],{},"SELECT\n",[390,398,400],{"class":392,"line":399},2,[390,401,402],{},"  date_trunc('day', created_at) AS day,\n",[390,404,406],{"class":392,"line":405},3,[390,407,408],{},"  model,\n",[390,410,412],{"class":392,"line":411},4,[390,413,414],{},"  sum(input_tokens + output_tokens) AS tokens,\n",[390,416,418],{"class":392,"line":417},5,[390,419,420],{},"  avg(duration_ms) AS avg_ms,\n",[390,422,424],{"class":392,"line":423},6,[390,425,426],{},"  percentile_cont(0.95) WITHIN GROUP (ORDER BY duration_ms) AS p95_ms,\n",[390,428,430],{"class":392,"line":429},7,[390,431,432],{},"  sum(CASE WHEN ok THEN 0 ELSE 1 END)::float \u002F count(*) AS error_rate\n",[390,434,436],{"class":392,"line":435},8,[390,437,438],{},"FROM llm_calls\n",[390,440,442],{"class":392,"line":441},9,[390,443,444],{},"WHERE created_at > now() - interval '7 days'\n",[390,446,448],{"class":392,"line":447},10,[390,449,450],{},"GROUP BY 1, 2\n",[390,452,454],{"class":392,"line":453},11,[390,455,456],{},"ORDER BY 1 DESC, tokens DESC;\n",[11,458,459,460,463],{},"(Adapt ",[40,461,462],{},"percentile_cont"," for SQLite—use a window approximation or export to BI.)",[53,465,467,468,470],{"id":466},"align-metrics-with-vendor-usage-and-optionally-opentelemetry","Align metrics with vendor ",[40,469,158],{}," and (optionally) OpenTelemetry",[61,472,473,505,536],{},[64,474,475,478,479,481,482,484,485,488,489,494,495,498,499,146,502,504],{},[15,476,477],{},"Vendor payloads",": when available, persist raw ",[40,480,158],{}," slices (prompt\u002Fcompletion\u002Fcached) so invoices reconcile with logs. OpenAI Chat\u002FResponses objects expose ",[40,483,158],{},"; with prompt caching you may see ",[40,486,487],{},"prompt_tokens_details.cached_tokens","—see the ",[20,490,493],{"href":491,"rel":492},"https:\u002F\u002Fplatform.openai.com\u002Fdocs\u002Fapi-reference\u002Fchat\u002Fobject",[24],"Chat object reference"," plus current caching guides. Do the same for Anthropic\u002FGoogle: normalize into ",[15,496,497],{},"your"," ",[40,500,501],{},"llm_calls.input_tokens",[40,503,149],{}," columns.",[64,506,507,510,511,192,514,192,517,520,521,526,527,531,532,535],{},[15,508,509],{},"OpenTelemetry GenAI",": client span conventions (",[40,512,513],{},"gen_ai.operation.name",[40,515,516],{},"gen_ai.request.model",[40,518,519],{},"gen_ai.usage.*",", …) are evolving—worth adopting if you already standardize on OTel. Start here: ",[20,522,525],{"href":523,"rel":524},"https:\u002F\u002Fopentelemetry.io\u002Fdocs\u002Fspecs\u002Fsemconv\u002Fgen-ai\u002Fgen-ai-spans",[24],"Gen AI spans"," (spec was ",[528,529,530],"em",{},"development\u002Fexperimental"," when researched; confirm semver + ",[40,533,534],{},"OTEL_SEMCONV_STABILITY_OPT_IN"," on older SDKs).",[64,537,538,541],{},[15,539,540],{},"RED alignment",": one “inference” span per model call with stable attributes; keep prompt bodies off prod spans unless you have a redaction pipeline.",[53,543,545],{"id":544},"summary","Summary",[11,547,548,549,552,553,256,556,559],{},"Track ",[15,550,551],{},"duration, tokens, outcome, and step"," per invocation; compute ",[15,554,555],{},"p95",[15,557,558],{},"error rate","; reconcile with published pricing. Everything else is polish, not a prerequisite for production sanity.",[561,562,563],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":386,"searchDepth":399,"depth":399,"links":565},[566,567,568,569,570,571,572,574],{"id":55,"depth":399,"text":56},{"id":94,"depth":399,"text":95},{"id":262,"depth":399,"text":263},{"id":304,"depth":399,"text":305},{"id":338,"depth":399,"text":339},{"id":378,"depth":399,"text":379},{"id":466,"depth":399,"text":573},"Align metrics with vendor usage and (optionally) OpenTelemetry",{"id":544,"depth":399,"text":545},"\u002Fblog\u002F03_ai_pipeline_tracing_costs_mvp_1x.webp","Which metrics actually matter for LLM pipeline cost and reliability in production, before you buy an enterprise observability stack.",false,"md","en",{},true,"\u002Fen\u002Fblog\u002Fai-pipeline-tracing-costs-mvp","2026-05-14",{"title":5,"description":576},"Minimal tracing for production AI pipelines","ai-pipeline-tracing-costs-mvp","en\u002Fblog\u002Fai-pipeline-tracing-costs-mvp",[589],"mvp","QWox8GnofZyP2B-z7HfZRE9_XLaigHwW0hJt2q7-S4g"]