Get your API key

Lesson 4 of 5

Limits and costs

Four cost walls: max_tokens, trimmed history, per-user rate limits and the key's spend limit; cost per user; prompt injection basics.

9 min Log in to open
One step at a time, like flipping pages.

By the end of this lesson your bot will have four walls against a surprise bill, you will know what one chat and one user cost you per month, and your bot will shrug off the most common tricks users try.

You need: the streaming chat from lesson 3, your coding agent, the AllModelAPI dashboard · Time: 9 minutes

  1. A chat app spends money every time someone presses Send. Most of the time that is a few paise. The danger is the rare bad day: one user pasting a whole textbook, a bug that loops, or a stranger with a script. You do not stop that with one big rule. You stop it with four small walls, each catching what the one before missed.

    A card titled Four cost walls with four rows: per answer max_tokens 600; per chat send only the last 10 messages; per user 20 messages an hour and 60 a day; per app a key spend limit of 5 dollars a month with an alert at 80 percent.
    Each wall is cheap to build. Together they turn "unlimited" into a number you chose.
    Why?

    Every wall alone has a gap. A spend limit stops the money but also stops your app for everyone. A per-user limit keeps one heavy user from causing that.

Log in free to read the rest of this lesson

Every lesson is free. A free account unlocks all of them and saves your progress.

Still to come in this lesson:

  1. Walls 1 and 2: cap each answer and the history
  2. Wall 3: limit each user
  3. Wall 4: the key's spend limit, and watching usage
  4. What your app costs, and what to charge
  5. Tricks users will try
  6. Recap