Case file 01AI · Performance9 min read

The chat felt slow. The frontend was only 3% of the wait.

How to measure every second of an AI chat message, the cause nobody had changed, and how we made the chat almost twice as fast without making the answers worse.

Read by an AI voice of Sajib. Words light up as he reads.

Frontend share

3%

of the wait, roughly. The rest was model calls in a row.

First words

5.4 → 2.9s

Answer finished: about 13s → 9s.

Blind judges preferred

128 : 31

New setup vs production. Serious errors: 1 vs 14.

A few weeks ago, people on our team started saying the same thing. “The AI chat feels slow.” “It used to be faster.” Nobody could say when it changed, or which messages were slow.

I work mostly on the frontend of this product, an AI chat assistant. When a user stares at a loading spinner, the frontend is the first suspect.

Instead of guessing, I wanted to know where every second went, from the moment the user clicks Send to the moment they can type again. Below is how to measure that, what the measurements found, and how we made the chat almost twice as fast without making the answers worse.

How a message travels

When you send a message, the browser sends it to our Next.js server. That server passes it to the backend. The backend does some work, calls an AI model, and streams the answer back word by word. The browser shows each piece as it arrives.

The streaming uses server-sent events (SSE). SSE is a simple way for a server to keep a connection open and push small messages to the browser, one after another.

So a slow answer could come from many places: the browser, the Next.js server, the backend, the database, or the AI model. I needed to see all of them at once.

Browser
Next.js server
Backend
AI model
answer, streamed in small pieces (SSE)
Fig. 1.1 · A message goes through three hops. The answer streams back in small pieces.

Putting everything on one clock

Every message gets an ID, and the ID travels with the request. At every step, on both the frontend and the backend, the code writes one line to a log file with that ID and the current time.

Because I ran everything on one machine, all the timestamps came from the same clock. That meant I could line them up and see exactly how long each step took.

The logging function is short on purpose:

function logStep(traceId: string, step: string) {
  if (!traceId || process.env.NODE_ENV === "production") return;
  const line = JSON.stringify({ traceId, step, time: Date.now() });
  appendFileSync("steps.log", line + "\n");
}

On the backend, I called this at every important point: when the request arrived, before and after each database query, before and after each AI model call, and every time a piece of the answer was sent. The logging was off in production and never committed; it existed only for this study.

On the frontend, I wrote a script with Playwright, a tool that controls a real browser from code. The script logged in, typed real messages, and recorded when the request left, when each piece of the answer arrived, and when the text actually appeared on screen.

Testing with real messages

A greeting and a 20-page PDF can take very different paths through the system, so I wrote 23 test scenarios that looked like real use:

  • short questions and greetings
  • questions that need a web search
  • long writing requests
  • a 3,000-word text pasted into the chat
  • a five-message conversation about a long document
  • files: PDF, Word, Excel, CSV and images

I ran each scenario twice: once through a real browser, and once by sending the message straight to the backend with no browser at all. That gave me 46 real conversations, with no errors.

What the numbers said

The median time from clicking Send to seeing the first words was about 5.5 seconds. The median time until the answer was completely finished was about 12 seconds.

The frontend’s share of that wait was about 3%.

Before the first word appeared, the frontend added only about a third of a second. Most of that came from the Next.js development server, which is slower than a production build. Rendering each new piece of text took about 50 milliseconds.

The other 97% was in the backend, and almost all of it was AI model calls.

Many small steps, one after another

When I looked at the backend logs, I saw a chain. A normal message went through several steps, and most of them called an AI model:

  1. A small, fast model read the message first and answered a list of yes/no questions about it. For example: does this need a web search? Does it need the earlier conversation? This took about 2.3 seconds, and nothing else could start until it finished.
  2. Then came searches, like a web search or a search in our knowledge base.
  3. Then the main model wrote the answer. It took about 3 seconds before its first word.
  4. After the answer had fully arrived, another model call cleaned up the formatting. This took another 2 to 7 seconds. Until it finished, the user could read the answer but could not send a new message.

No single step was terribly slow, but they ran one after another, so their times added up.

▼ First words
Can type again ▼
Frontend
~0.3s
Yes/no step
2.3s
Searches
0.5–2s
Model thinking
~3s
Streaming
2–3s
Formatting
~2.4s
Fig. 1.2 · The frontend was about 3% of the wait. The rest was model calls in a row.

The cause that nobody had changed

The most interesting finding was about the main model. Its first word came after 1 second on a short question, but after 10 seconds on a long PDF. For one Word document, it took 34 seconds.

That kind of delay usually means the model is “thinking”. Some newer AI models can reason silently before they answer, and the user sees nothing while they do.

A month earlier, the team had upgraded the main model to a newer version. The code sent exactly the same settings as before. But the new version had a different default: unless you told it otherwise, it thought at a high level before every answer. The longer the message, the longer it thought.

Nobody had changed that setting, because nobody knew it existed, and there was no line in our code to point at. This was the most likely answer to “why does it feel slower than before?”

I also found some smaller problems:

  • The yes/no step wrote out every answer in full, including all the “no” answers. Writing output is the slowest part of a model call, so this wasted time on every message.
  • For messages with files, the answer waited about 2 seconds for a file summary that it never used.
  • The user’s newest message was sent to the model twice. With a file attached, the whole file was sent twice.

Making it faster without making it worse

Making an AI answer faster is easy if you don’t care about quality. You can make the model think less, or use a smaller model. The hard part is proving that the answers are still good.

So I set up blind tests. For each change, I collected answers from the old version and the new version for the same messages. I mixed them into pairs labelled X and Y. Then an AI judge compared each pair without knowing which version was which. The judge had the original files, so it could check facts and numbers. Only after the judging did I reveal which answer came from which version.

AI coding agents did much of the work of running the tests and the judges. That is the only reason a study this size fit into a couple of days.

Here is what I tested:

  • Less thinking: I tried high, medium and low thinking levels. Medium was about a second faster than high, and the judges rated it slightly better overall.
  • A shorter yes/no step: I changed its instructions so it only wrote the answers that were different from the default. It went from 2.3 seconds to under 1 second, and it made the exact same decisions on every test message.
  • The file summary later: writing the file summary after the answer, in the background, made file messages about 1.6 seconds faster.
  • Checking links in parallel: checking all links in an answer at the same time saved a little more.

Together, these changes brought the time to first words down from about 5.4 seconds to about 2.9 seconds. The time until the answer was finished went from about 13 seconds to about 9.

Fig. 1.3 · The recommended changes, measured on the same test messages.

The one mistake that mattered

The blind tests found one serious problem. With less thinking, the assistant sometimes told users they could still meet a registration deadline that had already passed. High thinking caught this every time. Medium caught it only once in three runs.

For our users, a wrong date is much worse than a slow answer.

But paying for high thinking on every single message, just to catch dates, felt like fixing the problem in the wrong place. So I suggested a small code change instead: mark past deadlines as “passed” before the model ever sees them. Then no thinking level can get them wrong.

A harder test before handing it over

Before I recommended anything, I made the test harder. I grew it from 23 messages to 49, and the new ones were designed to trick the model. They included deadlines that had passed, date calculations, exact budget math, a hidden instruction inside pasted text, and a message from someone in distress.

Then I compared the recommended setup with production, again with blind judges. The new setup showed its first words about 2 seconds sooner. The judges preferred its answers 128 times, and production’s answers 31 times. They found 14 serious errors in production’s answers and only 1 in the new setup’s. The cost per message stayed about the same.

The new setup was not perfect. On some math questions, it started with a wrong number and then corrected itself. I listed that as something to fix before shipping.

Handing it over

The backend belonged to another team, so I wrote them a handover document instead of a pull request. It had the causes, ranked by how much time they cost, the test results, and the risks.

I also wrote down two warnings I found while tracing. First, the yes/no step could not simply run at the same time as the searches, because the searches needed its answers. Second, the final “done” event was a contract with the frontend. The frontend used it to show the cleaned-up text and to unlock the message box. If the backend changed that event, the frontend had to change too.

I also wrote a plain-language version with no code in it for the rest of the team, because choosing a thinking level is partly a product decision.

The team accepted the recommendations, and a backend developer implemented them.

What I learned

Measure the whole trip before you blame any part of it. One ID per request and a timestamped log on each side was enough to turn a vague feeling into numbers everyone could agree on.

Your code can stay the same while your system changes. An upgraded dependency, here an AI model, can come with new defaults. If you call something over the network, watch how long each step takes over time.

Chains add up. Each step in the backend looked reasonable on its own, but users felt the total.

Speed work on AI features needs a quality check. Blind, paired comparisons were cheap to run, and they found the one mistake that really mattered.

If I did this again, I would look at production timing data first. The backend already recorded how long each step took. A quick look at that data, day by day, might have pointed to the model upgrade before I wrote a single line of test code.

Product names and some details are changed or left out. The engineering is real.