A Q&A app that can't price each answer is still a demo
This portfolio project writes a trace and a token count for every request to SQLite, then adds them up into cost on a dashboard.
In brief
- A full stack Q&A app has four layers. Traces and token counts should be recorded in the business logic layer, then written to storage.
- Tokens map directly to cost, so every request needs a token record carrying its trace_id and query type.
- OpenTelemetry's GenAI conventions for agents are still in Development status, so pin the spec version before building the dashboard.
The demo of an internal document Q&A app has just gone well. The answers were right. Then the client’s head of operations asks two questions: which passage of the documentation was that answer based on, and what will it cost each month if the whole company uses it?
If you cannot answer, what you have built is still a demo. The project below adds the missing piece: every request leaves behind a trace and a token count, and a dashboard adds those numbers up into cost.
For anyone aiming at an FDE role, this is the kind of exercise a portfolio should include. Making the app work is not enough; you also have to show how it works.
What you will build, and what you need
AWS defines full stack development as building both the frontend and the backend of an application. This Q&A app has all four layers as AWS describes them. The interface is where the user types a question. The API layer takes interactions from the frontend and passes them down to storage. The business logic layer is the core of the backend. The storage layer manages and stores the application’s data.
You need Python, SQLite (which ships with Python’s standard library), a web framework you already know and access to any LLM. The code below is a simplified version meant to show the structure. The two functions retrieve and call_llm are where you plug in your own retrieval and LLM SDK.
Step 1: design storage before writing a prompt
Most people building a first project store only the answer. Here storage needs two tables from the start: one recording each step of a request, one recording tokens.
CREATE TABLE traces (
trace_id TEXT, step TEXT, detail TEXT, created_at TEXT
);
CREATE TABLE usage (
trace_id TEXT, query_type TEXT,
input_tokens INTEGER, output_tokens INTEGER, created_at TEXT
);
Check: run .schema in sqlite3 and confirm both tables are there. Do not drop the query_type column: in step 4 the dashboard relies on it to break cost down by query type.
So where does the value of query_type come from? The simplest option for an exercise is to let users choose it in the interface, for example a dropdown with “policy lookup” and “contract comparison”. It is easy to build, but the numbers are only right when users pick correctly.
The second option is to assign it automatically in the business logic layer with a classifier: a few keyword rules, or one short LLM call. If you classify with an LLM, record that call in the trace and the usage table too, because it also consumes tokens. Start with the dropdown, keep the real questions, then use that same data to test a classifier later.
Step 2: the trace must capture the steps in between
A correct answer did not necessarily go through the correct steps. If you store only the final result, you cannot tell which passage the model relied on. IBM lists traces, alongside metrics and logs, among the data to collect from an LLM application; in a Q&A app, the value of a trace lies in those intermediate steps.
JetBrains gives the example of a ReAct loop: when each “thought” is recorded as text, you get a full trace of the reasoning. That lets you evaluate those steps even when the final answer looks right.
In a Q&A app, the “steps” are the incoming question, the retrieved passages and the answer. Traces and tokens are both recorded in the business logic layer:
# Simplified version: retrieve and call_llm are your own functions
import sqlite3, uuid
from datetime import datetime, timezone
db = sqlite3.connect("qa.db")
def now():
return datetime.now(timezone.utc).isoformat()
def write_trace(trace_id, step, detail):
db.execute("INSERT INTO traces VALUES (?,?,?,?)",
(trace_id, step, detail, now()))
def handle_question(question, query_type):
trace_id = str(uuid.uuid4())
write_trace(trace_id, "receive_question", question)
passages = retrieve(question)
write_trace(trace_id, "retrieve", " | ".join(passages))
result = call_llm(question, passages) # returns text and token counts
write_trace(trace_id, "answer", result["text"])
db.execute("INSERT INTO usage VALUES (?,?,?,?,?)",
(trace_id, query_type, result["input_tokens"],
result["output_tokens"], now()))
db.commit()
return {"trace_id": trace_id, "answer": result["text"]}
The API endpoint only needs to call handle_question and return JSON to the interface. Remember to return the trace_id as well. When a user reports a wrong answer, you can find the matching trace at once instead of guessing.
Check: send a question, then run SELECT step FROM traces WHERE trace_id = '...'. The correct result is three rows, in the order receive question, retrieve, answer.
Step 3: money is counted in tokens
“What will it cost each month” can only be answered if you count tokens. IBM recommends tracking the number of tokens processed, especially when tokens are tied to model cost.
On the request side, the prompt’s token count is the main source of cost, according to a guide to OpenTelemetry’s GenAI conventions on the OpenObserve blog. In OpenTelemetry, that number is the span attribute gen_ai.usage.input_tokens.
So when you move from SQLite to OpenTelemetry, your input_tokens column maps to that attribute. To get a price, multiply tokens by the unit price in your model provider’s price list. Keep the unit price in a config file rather than hard-coding it.
Step 4: the dashboard must answer three questions
A single total cost figure does not tell you where to fix things. Small waste adds up to a lot over time, so JetBrains advises tracking tokens continuously and breaking cost down per request, per query type and over time. Those three cuts are three queries:
-- By query type
SELECT query_type, COUNT(*) AS request_count,
AVG(input_tokens) AS avg_input, SUM(input_tokens) AS total_input
FROM usage GROUP BY query_type;
-- By day
SELECT substr(created_at, 1, 10) AS day,
SUM(input_tokens), SUM(output_tokens)
FROM usage GROUP BY day ORDER BY day;
-- The 10 most expensive requests, with trace_id to open the trace
SELECT trace_id, query_type, input_tokens
FROM usage ORDER BY input_tokens DESC LIMIT 10;
A hypothetical example shows why the breakdown by type matters. A policy lookup question uses 2,000 input tokens on average. A question comparing two contracts pulls in more passages and uses 6,000. With 1,000 comparison questions a day, the 4,000-token difference per question alone comes to 4,000,000 tokens a day.
The third query helps you find the cause. Open the trace of the most expensive request and you will see whether the retrieval step pulled in too many passages or the prompt was duplicated. To cut cost, fix that exact step.
The dashboard interface can be a single page that runs these three queries and renders tables. For an exercise, that is enough.
Mistakes that make the dashboard lie
The first mistake is recording only the final answer. The dashboard then has cost figures but cannot explain why cost is high, because the trace lacks the retrieval step.
The second is copying OpenTelemetry attribute names from the web without pinning a version. According to OpenObserve, the conventions for agents and tool orchestration are still in Development status. OpenTelemetry’s official GenAI metrics spec page has also moved to a separate semantic-conventions-genai repository, so record the spec version you use in the README and look up current names in the new repository.
The third is confusing evaluation with observability. JetBrains draws the line this way: evaluation determines whether an agent can do the job, while observability determines whether it is doing it. A cost dashboard does not replace a quality test suite. You need both.
What this skill looks like at a client site
At a client, the first thing to do is ask which dimension they want cost broken down by: department, document type or user. The answer decides the columns in the usage table, before you write a single line of prompt.
When reading a job description that mentions operating or monitoring LLM applications, put this project first. On your CV, do not write “built a RAG chatbot”. Write that you built a full stack Q&A app in which every request has a three-step trace and a cost dashboard by query type, with a link to the repo.
A personal project with these four layers and two data tables shows you understand that clients are not just buying answers. They also need to know where the answers come from and what they cost.
Was this article useful?
Thanks for the feedback!