Chat and Summaries
Streaming chat on Workers AI, conversation history, document summaries on Queues, prices and limits.
Model
Chat, summaries and prompt enhancement use TEXT_MODEL in packages/ai/src/text.ts, currently @cf/google/gemma-4-26b-a4b-it on Workers AI through the AI binding. No model key is needed. In mock mode, mockText returns a deterministic answer so the chat works offline.
Changing the model means checking its input format, streaming output, context length, cost and the tests together; the template is not a gateway that configures every provider automatically.
Chat
POST /api/chat:
{
"prompt": "Write three short taglines for an AI image app",
"requestId": "e60b2e4b-32b1-42a5-bfd0-dbb20ecb37e9",
"history": [{ "prompt": "Earlier question", "answer": "Earlier answer" }]
}promptis 1 to 16,000 characters.requestIdis optional; when present it must be a UUID, created per message.historyholds up to 8 recent turns (4,000 characters per prompt, 8,000 per answer) so the model keeps context. The studio at/app/aisends it automatically.- The response is
text/event-stream. Read it chunk by chunk and handle a broken stream; do not callresponse.json(). The studio renders Markdown as it arrives.
Each message costs quotas.chatCredits (1). The server checks the balance and writes a generation row that reserves the credit before calling the model. A finished answer completes the generation; a failed or broken stream releases the credit once.
Summaries
- Upload your own
text/plainortext/markdownfile at/app/files(up to 1 MiB). - Call
POST /api/summarywith{ "fileId": "your-owned-file-uuid" }. - The response is the scheduled task, not the summary. The job runs on the JOBS queue; follow its state in the workbench.
A summary costs quotas.summaryCredits (5) and reads the first 16,000 characters. It does not split long documents, OCR images or parse arbitrary PDFs.
Verify
- Empty or overlong prompts, too few credits and files owned by someone else all fail without a charge.
- A normal chat streams and leaves one ledger line; an interrupted stream leaves the balance unchanged.
- A summary succeeds once even when the queue redelivers the message.
Continue with Credits and cost control and Testing.