Lesson 2 · Unit 04
The serving API
Streaming, stop reasons, retries and pushing back
What you practise
- Streaming responses, stop conditions and token limits, timeouts and retries, backpressure and rate limits, what the client should do with each
Try a question from this lesson
Pick one
An answer takes 8 seconds to generate. Version A sends it all at the end. Version B sends each token as it is made. Both finish together. Which feels faster, and why?
Correct answer
B: the user is reading after the time to first token
Why
With streaming the user starts reading after the time to first token, perhaps half a second, instead of watching a spinner for 8. Nothing got faster. The wait just moved to where a human can use it.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A stream of events
A common way to stream over HTTP is server-sent events. The response stays open and the server writes one small event per chunk.
Each event is a data: line followed by a blank line. The blank line is what tells the client the event is complete.
Content-Type: text/event-stream
data: {"text": "Hel"}
data: {"text": "lo"}Concept
Backpressure
A server that accepts everything queues without limit, and work that waits longer than its client will wait is wasted when it finally runs.
So queues are bounded. When full, the server says no at once: 429 or 503, often with a Retry-After header saying when to come back.
HTTP/1.1 429 Too Many Requests
Retry-After: 510 more exercises in the app
- 2× type the answer
- 4× multiple choice
- 1× match the pairs
- 1× true or false
- 1× fill the blanks
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.