Get a Key and Set a Spending Limit

In the previous lesson, you watched a model take text in and send predicted text back. Now you want to send a request of your own for the first draft reply to one of the tea shop’s customer emails. Before your script can send anything, the provider needs two things from you. It needs to know who’s calling, and it needs a way to bill them.

In this lesson, you’ll create a key with the provider you choose, cap what your account can spend in a month, and see what following this course costs, in cents. Every request carries the key, so start there.

Create a Key

A provider’s API is a paid service on the internet, so every request has to say which account it belongs to. That’s the job of an API key: a long secret string that your script sends along with each request. The provider looks the key up, finds your account, runs the request, and adds its cost to your bill.

A key works like the number of a prepaid card. Whoever holds it can spend your credit, and nobody asks for a PIN. Unlike a card, though, a key costs nothing to replace: you can delete it and create a new one in a few seconds. Treat it like a password from the moment you create it.

You need a key for one provider only, OpenAI or Anthropic. Every project in this course works with either, and the coding exercises need no key at all, because they run against a stand-in model. If you already have an account with one of them, pick that one.

Open the version for your provider. Both follow the same three steps of signing in, adding credit, and creating the key:

  1. Sign in at platform.openai.com, or create an account there. This is the API platform, which is billed separately from a ChatGPT subscription.
  2. Add credit on the billing page. The API is prepaid: you buy credit, and each request uses some of it. The smallest purchase is $5, which is far more than this course needs. Auto-recharge is on by default during setup. Turn it off, so the balance can’t quietly top itself up.
  3. Create the key on the API keys page. Create a new secret key and give it a name you’ll recognize later, such as tea-inbox-course. Copy the key, which starts with sk-, as soon as the page shows it.
  1. Sign in to the Claude Console, or create an account there. The Console is billed separately from a Claude chat subscription.
  2. Add credit in the Console’s billing settings. The API is prepaid: you buy credit, and each request uses some of it. Requests fail until your account has credit. If auto-reload is on, as it can be on an existing account, turn it off under Billing → Manage auto-reload, so the balance can’t quietly top itself up.
  3. Create the key on the API keys page. Click Create key. If the dialog first offers other ways to connect, click Continue with an API key. Give the key a name, such as tea-inbox-course, choose yourself as the linked account, and set the scope to your default workspace. Pick an expiration that outlasts the course: you can’t change it later, and an expired key has to be replaced. Copy the key, which starts with sk-ant-, because the Console shows the full key only once.

Keep the key somewhere private until the next lesson: a password manager is ideal. Don’t paste it into your code, any file you might commit or share, a chat window, or a message to a colleague. The next lesson gives it a file of its own that stays out of your repository. If you lose the key, or think someone else has seen it, delete it on the same page and create a new one.

Set a Spending Limit

A key on its own puts no ceiling on what your account can spend. You’ve written retry loops before, and you know how a loop with a wrong condition can keep going long after it should stop. A loop that calls a model spends money on every pass, and it keeps spending until someone notices.

A spending limit is that ceiling. It’s a monthly amount you set on your account. Once the month’s usage reaches it, the provider refuses further requests until the next month starts or you raise the limit. For this course, $5 a month is plenty, as the next section shows.

Set it now, before you write any code. Open the version for your provider:

  1. Go to the organization limits page.
  2. In Spend, select Edit spend limit.
  3. Enter 5 as the Monthly spend limit.
  4. Turn on Enforce a hard limit. Without it, the limit only sends you an alert, and requests carry on.
  5. Select Save.

Once the limit is reached, requests fail with a 429 status and the error code organization_spend_limit_exceeded. That’s the same status a provider sends for a rate limit, when you send too many requests or tokens in a short time.

The error code shows that waiting a few seconds won’t help, so your code shouldn’t retry the request. Enforcement isn’t instant, so the recorded spend can end up slightly over the limit.

  1. Go to the spend limits page.
  2. Next to your organization’s Monthly spend limit, click Set limit or Adjust limit.
  3. Enter 5 and save it.

Once the limit is reached, requests fail with a 400 status and an invalid_request_error whose message starts with “You have reached your specified API usage limits”. The message also says when access resumes.

Your prepaid credit is a second ceiling. Together, they keep your bill near a known, small maximum. When you reach either ceiling, requests fail with an error message. A runaway loop can still use up whatever is left of your $5 limit or your balance, and a little more, because neither ceiling stops requests the instant it’s crossed. Your scripts will meet errors like these later in the course, where you’ll handle them in code.

Estimate What the Course Costs

With a limit in place, you’ll want a number to compare it with. Providers charge per token, a chunk of text a model reads or generates. A token is often a short word or part of a longer one.

Each provider publishes a price per million tokens, with one price for the tokens you send and a higher one for the tokens the model generates. Generated tokens include any reasoning the model does before the reply you see, so the bill can count more output than the visible text.

For a concrete number, here’s one real call: a draft reply to a sample email from a customer whose parcel’s tracking hadn’t changed in six days. Open the version for your provider to see what that call cost:

The real call used 103 input tokens and 74 billed output tokens. The model this course uses for drafting costs $2 per million input tokens and $10 per million output tokens. That makes the draft cost about 0.09 cents, or roughly a tenth of a cent.

The model this course uses for drafting costs $2 per million input tokens and $10 per million output tokens. If a draft used about 100 input tokens and 75 output tokens, it would cost roughly a tenth of a cent.

Your real count can be higher: the model’s billed output includes any thinking it does before the reply, not only the text you see. Check the usage reported by your own calls. You’ll print it in later lessons.

A tenth of a cent per call adds up slowly. Following the whole course means roughly a hundred calls like this one: a single email in most lessons, and the batch of a dozen emails, run a few times at the end. That comes to around ten cents, so the $5 limit leaves plenty of room for experiments. Your total depends on how often you rerun examples, and prices change, so check your provider’s usage page now and then.

These calls are cheap only because the emails are short and the replies are capped. A feature that sends long documents, or runs for thousands of customers, costs far more. That’s why later in this course you’ll make every call report its own token usage and cost.

To work out what limit a feature like that would need, use the button below. It opens a chat with the mentor, an AI assistant, with this question filled in, so you can describe your team’s feature and follow up:

Check Your Setup

Before you move on, answer these questions for yourself:

  1. Your batch script has a bug that sends the same dozen emails again and again, all night. What stops the spending, and what does your script receive once spending stops?
  2. Why turn off automatic top-ups on your credit?
  3. Where should your key live until the next lesson, and what do you do if you think someone else has seen it?
  1. The spending limit stops it: once the month’s usage reaches your limit, the provider refuses further requests, and your script gets an error response instead of a draft. Your prepaid credit is a second ceiling behind it. Neither is instant, so a little usage can slip past.
  2. With automatic top-ups on, your account buys more credit when you run out, so the balance stops acting as a ceiling. With automatic top-ups off, your credit is a second limit behind the spending limit.
  3. Keep your key somewhere private, such as a password manager, and never in your code, a file you might commit or share, or a chat. If it might have leaked, delete it on your provider’s API keys page and create a new one.

You now have a key for your provider, a spending limit on the account, and a rough idea of what the course will cost. In the next lesson, Load Settings From the Environment, you’ll get that key into your script without writing it into the code.

Become a Member to join the conversation.