0Pricing
MLOps Academy · Lesson

Trace and Monitor LLM Calls

Capture tokens, latency, and cost per request.

Trace and Monitor LLM Calls is a free MLOps Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MLOps Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

You Cannot Fix What You Cannot See

An LLM app feels like a black box until you log what it does. Tracing records each call so you can debug, cost, and improve it later. 🔍

Capture the Request

For every call, log the final prompt, the model name, and key parameters. This request record lets you reproduce any answer exactly.

log.info({"model": "gpt-4o", "prompt": prompt, "temperature": 0.2})

Capture the Response

Save the generated text alongside the request. Together the input and output form one trace you can replay, score, or share with a teammate.

Record Token Usage

Each response reports tokens in and out. Logging tokens per call is how you attribute cost and spot prompts that quietly grew too large.

usage = response.usage
log.info({"in": usage.input_tokens, "out": usage.output_tokens})

Turn Tokens Into Cost

Multiply token counts by the price per token to get spend. Tracking cost per request keeps a runaway feature from surprising you on the bill.

cost = inp / 1e6 * 2.50 + out / 1e6 * 10.00

Measure Latency

Time each call from send to final token. Watching latency per request reveals slow prompts and tells you when streaming would help users.

start = time.perf_counter()
response = client.responses.create(...)
latency = time.perf_counter() - start

Spans Build a Trace

A real request may chain retrieval, the LLM, and a tool. Each step is a span, and nesting them into one trace shows where time and tokens go.

Tools Built for LLMs

Platforms like LangSmith, Langfuse, and Phoenix capture traces with one wrapper. A purpose-built observability tool beats parsing raw logs by hand.

from langfuse.openai import openai
response = openai.responses.create(model="gpt-4o", input=prompt)

Tag Traces With Metadata

Attach a user id, prompt version, and feature name to each trace. This metadata lets you slice metrics and find which segment is misbehaving.

Sample What You Cannot Store

At high volume, logging every full payload is costly. Sampling a fraction of traces keeps insight while controlling storage and privacy.

Dashboards and Alerts

Roll traces into dashboards for cost, latency, and error rate, then alert on spikes. Now production problems reach you before users complain.

Quick Check

Your monthly LLM bill jumped with no traffic change. Which logged value explains it fastest?

Recap

You can now trace LLM calls end to end: request, response, tokens, cost, latency, and spans, then watch them on dashboards. Eyes wide open! 🎉

Frequently asked questions

Is the “Trace and Monitor LLM Calls” lesson free?

Yes — the full text of “Trace and Monitor LLM Calls” is free to read here on the web, and the MLOps Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MLOps Academy course, upgrade to CoddyKit PRO.

What will I learn in “Trace and Monitor LLM Calls”?

Capture tokens, latency, and cost per request. You practise MLOps Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MLOps Academy?

No prior experience is required. MLOps Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Trace and Monitor LLM Calls” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MLOps Academy lesson?

Yes. Every MLOps Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. How LLMOps Differs from Classic MLOps
  2. Version Prompts and Evaluate Outputs
  3. Trace and Monitor LLM Calls
  4. Guardrails and RAG Evaluation
← Back to MLOps Academy