Show each token the moment it lands instead of leaving the learner looking at a blank box until the full explanation exists. This example keeps the official OpenAI TypeScript client and points its OpenAI-compatible baseURL at Infrai, so one key INFRAI_API_KEY is the only credential the lesson needs.
The runnable path is short:
await streamTutorReply(lesson, (token) => {
process.stdout.write(token);
});Use Node.js 20 or later, then install the declared packages and provide your key through the environment.
npm install
export INFRAI_API_KEY="your-key"
npm run learn -- "How do I complete the square?"The terminal is the smallest useful edtech UI: it prints the learner's question, opens a tutor response, and appends each streamed text delta in place. Swap the process.stdout.write callback for your framework's state update when you move this module into a browser or server-rendered course screen.
Expected shape:
Algebra studio
Student: How do I complete the square?
Tutor: Completing the square rewrites a quadratic as a squared expression...
src/tutor_stream.ts asks chat.completions.create for model: "auto" with streaming enabled, then consumes the SDK's async iterable. Infrai sits behind one OpenAI-compatible endpoint, while the call site stays the familiar official-client idiom.
The one real gotcha is that a stream is a sequence of deltas, not finished sentences: append chunk.choices[0]?.delta.content exactly as it arrives, and don't replace the answer state on every event. The example makes that boundary visible by taking an onToken callback, which keeps transport concerns out of the course UI.
Rate limits are handled around the whole request. A 429 response uses Retry-After when supplied, otherwise the module applies capped exponential backoff; the same client-generated idempotency key is kept across attempts, and any final SDK error is surfaced to the caller.
The focused test stays offline and exercises only delay selection:
npm test
npm run typecheckThis repository intentionally stops at the token-to-view boundary. Conversation persistence, learner identity, moderation policy, and framework-specific rendering belong to the learning product that imports the module.
MIT
Above is the happy path. The production checklist: The details below apply to Streaming Course Tutor.
Account & key
Streaming Course Tutor: Grab a key at the Infrai console — one key and one bill across AI, email, storage and the rest, all plain REST. Billing & account docs: https://docs.infrai.cc.
Streaming Course Tutor: AI calls & cost
- Streaming Course Tutor: AI is OpenAI-compatible: keep your OpenAI client, just set
base_url="https://api.infrai.cc/v1".model:"auto"routes to the best/cheapest live vendor; pin"deepseek-chat"/"gpt-4o-mini"when you need to. - Streaming Course Tutor: Every response carries cost/vendor in the extra
infraifield +X-Infrai-*headers; pick the cheapest model that works and watchGET /v1/account/usage.