Next.js App Router
Server route
Createapp/api/chat/route.ts. This validates the incoming shape, starts a Planck stream, and forwards the upstream body without buffering it.
app/api/chat/route.ts
request.signal allows a browser cancellation to propagate to Planck when the runtime supports request abort signals.
Client component
This component sends conversation history, decodes SSE frames, renders text deltas, handles stream errors, and exposes a Stop generating button.app/chat/page.tsx
FastAPI proxy
Install the server dependencies:StreamingResponse, which lets it preserve non-2xx errors instead of incorrectly starting a successful stream.
app.py
/api/chat route.
Production hardening
Useful request metadata
Add server-controlled headers when proxying to make production traffic easier to inspect:Streaming protocol
Review SSE chunks, usage, cancellation, and stream errors.
Tool calling
Add safe server-side functions to the chat loop.
Sessions
Group all turns from one conversation.
Custom properties
Attribute requests to environments, releases, or product features.