AI engineering traps (TypeScript) · Chapter 4 of 16

History that never ends

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Calling the model
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of AI engineering traps (TypeScript) are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.ts: run Mrs. Rizvi's morning on purpose

    Write reproduce() in repro.ts. Start one Conversation from sahayak.ts for Gomti Heights and ask it for 60 drafts in a row, the n-th one (n from 1 to 60) being, word for word: Draft a reminder about the water tank cleaning, note <n>. Stop at the first ask that throws, and resolve to how many asks were answered before it (60 if none threw).

    The check runs your reproduce() twice: beside the original sahayak.ts it must resolve to 52 (the 53rd ask is too big for the window), and beside a fixed sahayak.ts to 60. So it must really ask 60 times in one conversation. Conversation uses the lab model when you leave out its client; reproduce() must finish within 10 seconds.

  2. Task 2

    sahayak.ts: send only the history that fits

    Give Conversation's constructor a third parameter, maxHistoryTokens, 3000 when left out, and make ask send only what fits: the system message first, then the newest whole turns (a question and its reply) that fit, ending with the new question, so that countTokens of what is sent is at most maxHistoryTokens. Drop the oldest turns first, a whole turn at a time, never the system message, and send no fewer turns than fit.

    Keep the log line sahayak: sent <n> messages, <p> prompt tokens, and keep countTokens as it is. The check chats 30 turns with budgets of its own and counts the tokens of every request, and a short chat must be sent whole. It also type-checks sahayak.ts. Keeping the last 10 messages is not a fix.

  3. Task 3

    sahayak.test.ts: a test that would have caught it

    Write a vitest test file, sahayak.test.ts, that would have caught this bug. The check runs your file three times: beside the original sahayak.ts, where at least one test must fail; beside the fixed sahayak.ts, where every test must pass; and beside a wrong fix that keeps only the last 10 messages, where at least one test must fail.

    So have one long chat with the lab model (60 drafts) and expect every reply to be a draft: it starts 'Here is a draft you can send:'. Each test must finish within 10 seconds.

What you will learn

  • repro.ts: run Mrs. Rizvi's morning on purpose
  • sahayak.ts: send only the history that fits
  • sahayak.test.ts: a test that would have caught it

More in Calling the model

Build "History that never ends" yourself

Sunwai, a Lucknow app for housing societies, runs on a model, and Farah, its CTO, keeps assigning you tickets. Each is a trap AI engineering interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of AI engineering traps (TypeScript) are free. This one comes with the all labs plan.