One habit
Every AI feature should record tokens per request. If you cannot see usage, you cannot explain cost when your demo gets shared in a college WhatsApp group.
Tokens are what the model processes. Input, cached input, and output tokens can have different prices, based on the model.
What tokens mean
A token can be a character, part of a word, a whole word, punctuation, or spaces. For English, OpenAI gives a rough estimate: 1 token is about 4 characters, and 100 tokens are about 75 words.
Reasoning tokens may not appear in the visible answer. They still count toward output usage and are billed as output tokens.
Old token names
Many older tutorials use max_tokens as a general setting. Do not copy that blindly.
With the Responses API, use max_output_tokens. Chat Completions uses max_completion_tokens, but this track uses Responses because OpenAI recommends it for text generation apps.
Log usage
Add one small log line after your server route gets a response. Keep this on the backend, along with your API key.
Field names can vary by SDK response shape, so log the full usage object first. Then store only the numbers your app needs.
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
export async function POST(req) {
const { question } = await req.json();
const response = await client.responses.create({
model: "gpt-4o-mini",
instructions: "Answer like a helpful project mentor. Keep it short.",
input: question,
max_output_tokens: 300,
});
console.log("openai_usage", {
response_id: response.id,
usage: response.usage,
});
return Response.json({
answer: response.output_text,
usage: response.usage,
});
}Add a budget guard
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Estimate before building
Before coding a feature, write one sample input and one expected answer. Estimate tokens roughly using 100 tokens as about 75 English words.
Then check real usage from the API response. Use pricing from the OpenAI pricing page for your chosen model, because prices depend on model and token category.
Simple cost note
Put a short note in your README so reviewers know you thought about cost. Do not hard-code prices unless you plan to keep them updated.
Cost and token tracking
This app calls OpenAI from a backend route only.
Each request sets max_output_tokens.
The server logs the response id, model, route name, and usage object.
Cost should be estimated using the current OpenAI pricing page for the selected model.
During testing, also check the OpenAI Usage Dashboard.Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done