Skip to content
IT Vizag
← Python Meets AI

How Python talks to AI models

Lesson 1 of 10 · 3 min read

APIs, keys and requests, the big picture.

🎬 Video version coming soon — read it now

In AI Basics you learned what a large language model (LLM) is. Chat apps like ChatGPT, Gemini and Claude are just one way to use them. With Python you can use the same models from your own code: summarise 500 files overnight, build a study buddy, or add AI to an app.

The big picture

  1. Your Python program builds a request: which model, your instructions and the user's message.
  2. It sends the request over the internet to the AI company's API (application programming interface): a web address that programs, not people, talk to.
  3. The model generates a reply.
  4. The API sends back a response, as JSON, which the SDK turns into Python objects.

Every request carries an API key, a secret password that identifies your account and its billing.

SDKs make it easy

You could build the web request yourself with the requests package. Instead, each company publishes an SDK (software development kit): a Python package that handles the details. In this course we use Anthropic's SDK for Claude:

pip install anthropic

The ideas, including keys, messages, system prompts and JSON output, are almost identical for OpenAI and Google's Gemini SDKs. Learn one and the others take an afternoon.

Pick the provider that suits you

You don't need to pay to learn. Choose based on what's practical for you:

  • Google AI Studio (Gemini): a free API key with no card needed, and generous free limits for practice. The best starting point for most students.
  • OpenRouter: one key that reaches models from many companies, including several free models. Handy for comparing models.
  • Anthropic (Claude), OpenAI and others: paid per use, usually with strong quality. A good choice when you build something serious or your company provides a key.

The next two lessons show how to get a key and make your first call with each option. The examples later in the course use Claude, but every idea carries over: only the client setup and a few names change.

What the API gives you that the chat app doesn't

  • Automation: run the same prompt over thousands of items
  • Control: set rules, length and output format exactly
  • Integration: put the answer straight into a file, database, spreadsheet or website
  • Your own product: build tools others can use

What it costs

APIs charge per token (roughly ¾ of an English word) for both what you send and what you get back. Pricing is listed per million tokens on each provider's pricing page. A practice session of a few dozen short requests usually costs a few rupees, but a loop over thousands of large files can cost much more. The next lesson shows how to set limits so a bug can't surprise you.

Everything you've learned comes together

  • Variables, strings and f-strings build prompts
  • Lists and dictionaries hold the conversation
  • Functions wrap each AI call
  • try/except handles network errors
  • Files and JSON save the results
Tip: Keep the AI Basics lesson "Where AI goes wrong" in mind. The model behind an API makes the same kinds of mistakes as in the chat app, so your code must check its answers.

In one line

Your Python code sends a request with an API key to a model's API and gets a response back; an SDK handles the details.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend