Skip to content
engineering workbench.by StudyPort Academy
All projects
Advanced120 min

AI Content Assistant API

Capstone : layered routes, a model call, streamed drafts, real validation of a malformed model body, stored history with pagination, and an AI-only budget gate.

AI routesStreamingValidationPaginationCost control

YOUR BUILD CHECKLIST

  1. Start with a layered health route.
  2. Call the model stand-in over and return the draft text.
  3. Consume a streamed response and join the deltas.
  4. Detect a malformed model body and return 502 before any draft reaches the client.
  5. Store accepted drafts in MongoDB and list them with limit and offset.
  6. Refuse the model call when the next request would exceed the AI budget.

STAGE BRIEF

1. Core structure

Uses Structuring a real backend

Export an Express app with a small layered GET /health handler.

Open the related lesson

YOUR APP

Work in one file. When you move to the next stage, this code stays. Each Run still fires one request (or one stdout capture) against a fresh database when MongoDB is involved.

GET /health

Project mode: your code carries into the next stage. Each Run is still one request.

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

Solution walkthrough

Each stage has a finished file for that point in the project. Open it when you are stuck. Loading a starter or a solution is your choice; the editor does not overwrite your work unless you ask.

1. Core structure

Uses Structuring a real backend

Export an Express app with a small layered GET /health handler.

2. Call the model

Uses Calling an AI service from your backend

POST /drafts should call the local stand-in with a messages array and return the content string.

3. Streamed draft

Uses Handling streamed responses

Read SSE data lines, skip [DONE], and join deltas into draft.

4. Malformed model body

Uses Validating AI output before it reaches a user

The stand-in returns { note: "looks finished" }. Return 502 with a contract error. A crash or a 200 fails.

5. History with pagination

Uses CRUD operations, Headers, status codes, and pagination

Store accepted drafts with prompt and createdAt. GET /drafts supports limit and offset.

6. AI budget gate

Uses Rate limiting and cost control for AI endpoints

When spent + cost would pass the limit, return 429 before calling the provider.

Make it your own

The original works. These are your next experiments.

  • Optional: add a GET /drafts/:id route.
  • Optional: log budget refusals as with level warn.
Open a fresh playground