# Test LLM streaming endpoints: time to first token

> Test streaming LLM and AI agent endpoints (Server-Sent Events) with HITOP: events as they arrive, time to first token, each event's time since Send, and a CI check with hitop send --stream.

Source: https://hitop.rws8.tech/guides/test-llm-streaming-endpoints/

## Why streaming needs its own view

A chat or agent endpoint that streams answers with Server-Sent Events (`Content-Type: text/event-stream`) sends its first tokens long before the last ones. What people feel is the **time to first token**: how long they wait before anything appears. A client that waits for the whole body shows only the total, which hides it.

HITOP reads an event stream as it arrives and times it from the moment you press Send:

- **First byte**: the first byte of the body (often a comment or keep-alive line).
- **First event**: the first complete event, which for an LLM endpoint is the time to first token.
- **Each event's time** since Send (`+625 ms`), so gaps between tokens show.

## In the web app

1. Create a request: `POST https://api.openai.com/v1/chat/completions` (or your provider's or agent's streaming endpoint).
2. Headers: `Authorization: Bearer ${apiKey}` and `Content-Type: application/json`. Keep `apiKey` as a **secret** variable of an environment: it is masked as `••••` everywhere HITOP shows it.
3. Body, with streaming turned on:

   ```json
   {
     "model": "gpt-4o-mini",
     "stream": true,
     "messages": [{ "role": "user", "content": "Explain Server-Sent Events in one sentence" }]
   }
   ```

4. Send. The response shows **Live** and the events one by one, each with its time since Send, and above them the time to the first byte and to the first event. Stop ends the stream at any point; there is no time limit while it is live.

![HITOP's Events view after a stream: 19 events, first byte 12 ms, first event 631 ms, and each event with its time since Send](/llm-stream.png)

A response with `Content-Type: text/event-stream` is read live even without an `Accept: text/event-stream` header. After the stream ends, the events stay in the response's **Events** tab next to Body and Headers.

## From the terminal and in CI

`hitop send --stream` (CLI 1.3.0 and later) prints each event as it arrives with its time since the request went out, then a summary on stderr:

```
hitop send https://api.openai.com/v1/chat/completions --stream -X POST \
  --secret "key=$OPENAI_API_KEY" \
  -H 'Authorization: Bearer ${key}' -H 'Content-Type: application/json' \
  -b '{"model":"gpt-4o-mini","stream":true,"messages":[{"role":"user","content":"Say hi"}]}'
```

```
200 • 25ms
────────────────────────────────────────────────────────────
+   424 ms  {"choices": [{"delta": {"content": "Hi"}}]}
+   474 ms  {"choices": [{"delta": {"content": " there"}}]}
+   524 ms  {"choices": [{"delta": {"content": "!"}}]}
+   574 ms  [DONE]
────────────────────────────────────────────────────────────
headers 25 ms · first byte 424 ms · first event 424 ms · 4 events · 0.57 s
```

On your own machine, keep the key in the OS keychain instead of the command line: `hitop secret set --env openai key` asks for it once, and `--env openai` uses it. Secret values are masked in everything `hitop` prints.

A body that isn't an event stream (newline-delimited JSON, for example) is printed as it arrives, and the summary gives the time to the first byte. `--stream` has no time limit (Ctrl-C ends it) and doesn't run a script after the request, which needs the whole body.

To fail a CI job when the time to first token is too high, read it from the summary:

```sh
hitop send "$URL" --stream -X POST --secret "key=$OPENAI_API_KEY" \
  -H 'Authorization: Bearer ${key}' -H 'Content-Type: application/json' -b "$(cat request.json)" \
  > /dev/null 2> timing.txt
ttft=$(grep -o 'first event [0-9]*' timing.txt | grep -o '[0-9]*$')
echo "Time to first token: ${ttft} ms"
[ "$ttft" -le 2000 ] || { echo "Time to first token over 2 s"; exit 1; }
```

## What the times include

Times start when you press Send (in the CLI, when the request goes out), so a script before the request counts, as it does in the response time. They are wall-clock times on your machine: network, the provider's queue and the model's first token all add up in them, which is what a user waits for.
