AI API from Scratch: How It Works, Keys, Costs & First Request
Learn how AI APIs work: get your API key, understand token costs, send your first request in Python, and download 18 tested scripts to get started.

👁 113 reads
AI APIs give you access to models like GPT, Claude, and Gemini from your own code, not a chat window. You send a request with your key, get a JSON response, and pay for tokens used - usually fractions of a cent per request. This is the first stage of a path from zero to your own AI agent: below you’ll find a roadmap of the whole journey, step-by-step guides, current pricing, and a pack of ready-to-run scripts we tested on Windows.
Status as of October 4, 2026
- Cheapest models: GPT-6 Luna $0.10 / $0.50 and Gemini 2.5 Flash-Lite $0.10 / $0.40 per million input / output tokens.
- Free tier: Gemini API free tier (no card required) and 17 free models on OpenRouter (50 requests daily).
- Mid-range: Claude Sonnet 5.5 and GPT-6.1 Sol at $2 / $10.
- Our test: 18 scripts (Python, Node.js, curl) run on Windows 11; all runs of the entire cluster cost about 5 cents.
- Download: ai-api-starter pack with README.
Roadmap: From Zero to Your Own Agent
A reader asked us where to find a step-by-step explanation of how APIs work, how to build automations, and how to create a simple agent - not just copy code, but understand what’s happening. We couldn’t find such a path in one place, so we built it. It has three stages, each building on the previous:
Stage 1. API (you are here)
Stage 2. Automation
Stages 2 and 3 are collected in a separate guide AI Agents from Scratch. Background theory: LLM: how a large language model works, MCP, RAG and fine tuning.
What is an AI API
API (Application Programming Interface) is an established way for one program to request something from another. In the case of AI, your program sends a request over the internet with the model, messages, and parameters, and the provider’s server sends back generated text. No chat window, no conversation history, no subscription - you pay for the number of tokens, which are chunks of text on input and output.
A real request we sent on October 4, 2026 from a Windows terminal looks like this:
#!/usr/bin/env bash
# First request via curl (Git Bash, Linux, macOS). First: export OPENROUTER_API_KEY=sk-or-v1-...
curl -s https://openrouter.ai/api/v1/chat/completions
-H "Authorization: Bearer $OPENROUTER_API_KEY"
-H "Content-Type: application/json"
-d '{
"model": "openai/gpt-6-luna",
"messages": [{"role": "user", "content": "Napisz jedno zdanie o Krakowie."}]
}'
We tested it in Polish. The GPT-6 Luna model responded with one sentence about Krakow, used 16 input tokens and 36 output tokens, and OpenRouter charged $0.0000196 for it. A full explanation of the request, response, JSON, and error codes (401, 429, 500) is in the text What is an API? Explained with an AI example.
What it’s useful for in practice:
- summarizing emails, notes, and documents in the background, without copying to chat;
- extracting data from invoices, CVs, listings straight to a spreadsheet;
- chatbot on a website or in a messenger;
- classifying customer tickets and reviews;
- automations that run at night and leave a ready report every morning;
- agents that call tools themselves - that’s stage 3 of the path.
How to Start: Step by Step
- Understand the request and response. 15 minutes with the text What is an API and your first curl without programming.
- Create an account and key. For starters we recommend OpenRouter (one key for all models) or Google AI Studio (free, no card required). Instructions for four providers and security rules: API Key: how to get it and how not to burn it.
- Set a spending limit on your key. $2-5 per month is enough for learning. This is the only protection that really works when a key leaks.
- Install Python and send your first request. Venv, openai library, streaming, error handling and retries: First Request to AI API in Python. There’s also a Node.js version.
- Calculate how much it costs. AI Tokens and API Cost Calculator.
- Learn the parameters. System prompt, temperature, context window and max tokens with an experiment: API Parameters in Plain English.
- Receive data, not text. Structured output: how to force AI to respond in JSON - without this you won’t build automation.
- Choose a provider for the long term. OpenAI, Claude or Gemini API - comparison and OpenRouter guide.
Models and Providers
Each company has a family of models from cheap and fast to expensive and most powerful. To start, it’s enough to know one from each shelf:
| Provider | Cheap and fast | For most tasks | Most powerful | Free start |
|---|---|---|---|---|
| OpenAI | GPT-6 Luna | GPT-6.1 Sol | GPT-6 Astra | no |
| Anthropic | Claude Haiku 4.5 | Claude Sonnet 5.5 | Claude Opus 5.5, Fable 5.1 | small credits |
| Gemini 2.5 / 3.5 Flash-Lite | Gemini 3.8 Flash | Gemini 3.1 Pro (preview) | yes, Flash models | |
| DeepSeek | V4.1 Flash | V4 Pro | - | no |
| Mistral | Mistral Small 4 | Mistral Medium 3.5 | - | $10 credits in Free plan |
| OpenRouter (aggregator) | 466 models from all companies on one key | 17 free models | ||
We described provider details separately: Google AI Studio, DeepSeek, Mistral, Kimi, Grok API and Agents API from OpenAI.
API Pricing
In APIs you pay per million tokens, separately for input and output. Prices from October 4, 2026, in USD, excluding VAT:
| Model | Input | Output | 100 emails daily for a month* |
|---|---|---|---|
| GPT-6 Luna | 0.10 | 0.50 | $0.47 |
| Mistral Small 4 | 0.15 | 0.60 | $0.63 |
| DeepSeek V4.1 Flash (peak) | 0.30 | 1.20 | $1.26 |
| Gemini 3.8 Flash | 0.75 | 3.75 | $3.49 |
| Claude Haiku 4.5 | 1.00 | 5.00 | $4.65 |
| Claude Sonnet 5.5 | 2.00 | 10.00 | $9.30 |
| GPT-6.1 Sol | 2.00 | 10.00 | $9.30 |
| Claude Opus 5.5 | 4.00 | 20.00 | $18.60 |
| GPT-6 Astra, Claude Fable 5.1 | 10.00 | 50.00 | $46.50 |
* Email summary: 800 input tokens and 150 output tokens, 100 times daily for 30 days. You’ll find a full table of 20 models with sources and an interactive calculator where you can enter your own numbers in the text AI Tokens: what they are and how much they cost. Two things the price list hides: reasoning models add “thinking” to output tokens (Gemini 3.8 Flash used 390 thinking tokens for a two-sentence response in our test), and Polish text takes more tokens than English.
AI API in Polish
All models from major companies understand and write in Polish. In our test of six models on the same question, each responded in correct Polish, differences were in style and format discipline. Polish has its price in tokens though. We sent the same paragraph to seven models in Polish and English:
| Model | Tokens PL | Tokens EN | Polish more expensive by |
|---|---|---|---|
| Gemini 3.8 Flash | 116 | 84 | 38% |
| GPT-6 Luna | 126 | 84 | 50% |
| Mistral Small 4 | 138 | 84 | 64% |
| DeepSeek V4 Flash | 145 | 84 | 73% |
| Claude Sonnet 5.5 | 214 | 121 | 77% |
| Claude Haiku 4.5 | 159 | 85 | 87% |
| Llama 3.3 70B | 177 | 84 | 111% |
Practical conclusion: when working with Polish texts, compare models by real request cost (the usage.cost field), not by the price list rate. Claude Sonnet 5.5 and GPT-6.1 Sol have identical pricing, but the same Polish text in Claude is about 1.7 times more tokens. The measurement method and script are in the text about tokens. All providers’ documentation is in English; in Polish you have this path.
Our Test: ai-api-starter Pack
We ran every code snippet from this path on our own computer and in the texts we show the real result, not what “should” come out. Environment: Windows 11 Pro, Python 3.13.15 (libraries openai 3.24.0, pydantic 2.13.5, tiktoken 0.14.0), Node.js 26.10 (openai 7.27.0), curl 8.22. Access to models through OpenRouter. We collected all scripts into one pack with a README and .env.example file without keys:
Download ready scripts: ai-api-starter.zip
Inside:
- 01-03 - first request with cost counter, streaming, error handling with retries and a mock server returning 429 for free testing;
- 04 - experiment with temperature 0 / 0.7 / 1.2 and the trap with GPT-6 that quietly ignores temperature;
- 05 - structured output: invoice data with Pydantic schema and customer review analysis;
- 06 - text tokenization and measuring the “Polish tax” on 7 models;
- 07 - system prompt and response truncation by max_tokens;
- 08 - list of OpenRouter free models;
- 09 - same question to 6 models with time and cost;
- 10 - same model at different providers: cheapest, fastest, without data logging;
- curl/, js/, dostawcy/ - terminal requests, Node.js version and code directly for OpenAI, Anthropic and Gemini.
Honestly about limitations: we didn’t run scripts from the dostawcy/ directory with real OpenAI, Anthropic and Google keys, only with fake ones (servers responded with authorization error, so libraries and code work). Everything else went through real models. The total cost of all runs in this path was about 5 cents.
Most interesting results from tests:
- at temperature 0 the model gave identical character-by-character responses three times, at 1.2 it invented the word “Mrugu” and broke a line in the middle of a name;
- the same question to six models cost from $0.00002 to $0.00313 - 150 times difference with similar quality;
- the same gpt-oss-120b model at four providers: from 0.4 s to 6.2 s and up to 5.6 times difference in price;
- the request “respond in JSON” without a schema returned JSON wrapped in a Markdown block, on which
json.loadscrashed.
API, Chat, Local Model or No-Code Tool
| API | Chat (ChatGPT, Claude, Gemini) | Local model (Ollama) | No-code (n8n, Zapier) | |
|---|---|---|---|---|
| Payment | per token | subscription | electricity and hardware | tool subscription + API tokens |
| Need to code | a bit (Python, JS) | no | a bit | no |
| Automation | full | limited | full | full, in ready blocks |
| Best models | yes | yes | no, only open | yes, through API |
| Privacy | depends on provider | depends on plan | data doesn’t leave computer | depends on providers |
| Good for | own applications, scale | manual work | sensitive data, experiments | connecting apps without code |
When a model on your own GPU is cheaper than API, we calculated in the text Local model or API. No-code tools are stage 2 of the path - under the hood they still send requests to APIs, so knowledge from this stage will be useful.
Who This Path Is For
- For people who use AI chats and want more - automation of repetitive tasks without manual copying.
- For developers from outside AI - quick entry into model specifics: tokens, parameters, costs, structured output.
- For small business owners - to calculate how much a chatbot or automation would really cost before you pay an agency.
- For students - everything can be practiced for free on free models.
You don’t need programming experience. It’s enough to be able to copy a command to the terminal and change text in quotes. If you prefer to start completely without code, read Automation with AI: where to start first.
Frequently Asked Questions
What is an API?
API is an application programming interface, a contract between programs: one sends a request in an established format, the other sends back a response. In AI it means access to models like GPT or Claude from your own code instead of a chat window.
How much does AI API cost?
You pay per token: from $0.10 per million input tokens (GPT-6 Luna) to $50 per million output tokens (GPT-6 Astra, Claude Fable 5.1). A typical request on a cheap model costs hundredths of a cent. You can calculate the cost of your scenario in the calculator.
Is AI API free?
Partially. Google has a free Gemini API tier for Flash models, and OpenRouter has 17 free models with a limit of 50 requests daily. OpenAI has no free tier, Anthropic gives small credits to start.
How to get an API key?
Create an account on the provider’s platform (platform.openai.com, platform.claude.com, aistudio.google.com or openrouter.ai) and create a key in settings. The key is shown in full only once. Details and security rules: API Key.
Does ChatGPT Plus subscription give access to API?
No. OpenAI API is a separate account with separate prepayment, same for Claude and Gemini.
What is OpenRouter?
An aggregator that gives one key to over 460 models from different companies, at provider prices, with a 5.5% commission on top-up. We tested the entire path on it: OpenRouter guide.
Do I need to know how to code to use API?
Not for first requests - curl or a ready script from the pack is enough. For your own automations, basic Python or a no-code tool that sends requests for you will be useful.
Which programming language to choose for AI API?
Python - it has the most examples and official libraries from all providers. JavaScript (Node.js) is the second choice, especially when you’re building a website or bot. Code in both looks almost the same.
Google · Your sourcesQuantslant higher in your Google - one clickAdd to preferred sources →Sources and Verification Date
We verified pricing and documentation on October 4, 2026. We ran scripts the same day on Windows 11 through OpenRouter.
More on this from QUANTSLANT
benchmarks·Cloudflare Clef: Open Decision Model Tested on RTX 4090Cloudflare released Clef and Clef-flash: open decision models starting at $0.038 per million tokens. Here is how they work, cost, and perform on local hardware.

