Module 3 · Your tool

AI inside the app: API key, cost per use and where the key never goes

Lesson 8 of 12 10 min lesson Updated on October 30, 2026
Progress in the course67%

In this lesson

Putting AI inside the app is easy. Doing it without leaking the key that pays the bill is what matters.

So far you have used AI to build. Now the app will use AI on the inside: a button that summarizes an email, classifies a message or suggests a reply. For that it needs an API key.

Chat and API: two ways to pay

In the chat, you pay a subscription and you talk. With the API, it is your program that talks to the AI, and you pay for what you use, measured in tokens (pieces of text). The app sends the request together with a key, the password to your account. They are separate accounts.

Check the current prices on the provider's official page.

Real example

An accounting firm gets dozens of long emails a day. A "summarize and list what the client is asking for" button saves minutes on each one. With a small model and a monthly limit, the cost stayed low and predictable.

The rule with no exceptions

Everything in a site's HTML, CSS or JavaScript is downloaded by every visitor. Anyone can open the source code and read it. If the key is in there, anyone can copy it and spend on your behalf. That is why the key lives on the side the visitor does not see.

SetupWhere the key lives
Local use (this lesson)An environment variable on your computer, outside the folder
PublishedIn a server function at the hosting service
Local model (Ollama)There is no key and no cost per use

Careful

If the AI suggests a .env file in the project folder, accept it only if the .gitignore already excludes it (lesson 9). When in doubt, use the system environment variable.

The request

You ask for two things: the page with the button and a small server that holds the key. The request tells the AI to read the key from the environment variable and ends with an audit: the AI confirms that the key is not in any file. Then do your own: open the files and search for a piece of the key with Ctrl + F.

The text the app sends leaves your computer and goes to the provider's servers. Send the minimum, replace CPF numbers (Brazil's taxpayer ID) and account numbers with placeholders, and read the provider's data policy.

What the pros know

Not every task needs an answer right away. Summarizing a thousand emails from the week, for example, can wait. Anthropic's official documentation describes a batch API (Message Batches) that cuts the cost by 50%, and says that most batches finish in less than an hour. The trade is simple: you give up the immediate answer. Before you pay a lot for a button that gets clicked all day long, ask: does this need to happen now?

Practice · 10 min

  1. On the provider's site, set the monthly limit and create the key. Keep it in your password manager.
  2. Open the tool in a new folder and paste the request below. Follow the instructions to set the environment variable and start the server.
  3. Test with three made-up texts and then audit the files, looking for the key.
Create an index.html with a text box and a "summarize" button, and a small local server that receives the text, calls the Claude API and returns a 3-line summary. The key must come from the ANTHROPIC_API_KEY environment variable and must NEVER appear in any file in this folder. Use the cheapest model that does the job. Limit the text to 4000 characters and show a clear error if the key is missing. Afterwards, explain how to run it and list the files to prove the key is not in them.
Deliverable: the "summarize" button working, the key absent from every file and the usage from your tests written down.

Summary

Sources

Read on October 30, 2026.

  1. Anthropic. Batch processing: 50% lower cost; most batches finish in less than 1 hour.

Lesson quiz

Three situations to apply what you learned.

Lesson 8 of 12

Before moving on to the next lesson

Put what you saw into practice. The boxes stay checked in this browser, so you can come back later.

Have a question about this lesson? The IV Help team answers personally on WhatsApp.

Ask on WhatsApp