Your practice partner now works end to end. The club’s page sends a member’s message, the server streams the reply with the key out of sight, each session keeps its history under a token budget, and Stop ends the call to the model. You built those pieces one lesson at a time, in the order the problems showed up.
In this project, you’ll build the partner again from a brief, start to finish, the way you’d build a feature at work. You’ll write it in the next exercise, where tests check each step against a stand-in model. Then you’ll bring it into your project and run a real practice session on your provider. Start with what the club asked for.
The Brief
The club meets every Saturday, and between meetups, members want to practice ordering at a café in Spanish. The practice partner plays a waiter in Madrid. Its instructions ask it to reply in Spanish and end each reply with one tip, in English, about a mistake in the member’s last message. The model usually follows them, and the page shows its tip as written.
The partner runs on the club’s page, so every member’s message costs the club money, and anyone can send one. Here’s what the finished partner has to do:
- Keep the key on the server: The page posts
{ sessionId, message }to the club’s ownPOST /api/partnerroute, never to the provider. - Refuse a bad request before it costs anything: A missing or over-long session ID, or an empty or over-long message, gets a
400and a line of text, with no call to the model. - Remember the session: The server keeps each session’s history in order, with the instructions sent separately, and answers a second message with
409while a reply streams. - Show the reply as it’s written: Display it piece by piece as text, with a note when the reply breaks off or hits the output limit.
- Stop the bill, not only the page: Stop ends the page’s request and the server’s call to the model. The stopped reply stays out of the history, and the server logs it as a stop, not a failure.
- Hold a long session at a steady cost: Before each call, drop the oldest exchanges until the request’s estimate fits a budget of 1,000 tokens.
The key must never reach the page, and the server’s call to the model must never run on after a member stops the reply. The provider still bills for the request’s input and any tokens it generated before the abort reached it.
The Starting Project
You’ll write the server’s route and the page’s code in the exercise after this brief, then copy them into the project you’ve grown during the course. Read this section now, and come back to it once the exercise’s tests pass. If you’ve followed along, your folder already has every piece: