Skip to content
IT Vizag
← Python Meets AI

Your first AI call from Python

Lesson 3 of 10 · 3 min read

Send a prompt and print the answer.

🎬 Video version coming soon — read it now

Let's send a prompt from Python and print the answer. Use whichever key you got in the last lesson. The three versions below do exactly the same thing.

With a free Gemini key (Google AI Studio)

!pip install -U google-genai
import os
from google.colab import userdata
from google import genai

os.environ["GEMINI_API_KEY"] = userdata.get("GEMINI_API_KEY")
client = genai.Client()     # reads GEMINI_API_KEY

MODEL = "gemini-4-hydrogen"  # copy a current model name from AI Studio's Models page
interaction = client.interactions.create(
    model=MODEL,
    input="Explain what a Python list is in two sentences.",
)
print(interaction.output_text)

With OpenRouter

OpenRouter speaks the same format as OpenAI, so you use the openai package and point it at OpenRouter:

!pip install openai
from google.colab import userdata
from openai import OpenAI

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=userdata.get("OPENROUTER_API_KEY"),
)

MODEL = "google/gemma-4-31b-it:free"   # pick any model marked free on openrouter.ai/models
response = client.chat.completions.create(
    model=MODEL,
    messages=[{"role": "user", "content": "Explain what a Python list is in two sentences."}],
)
print(response.choices[0].message.content)

Change MODEL to compare models with no other code changes. Free model lists change often, so if one stops working, pick another from the site.

With Claude (Anthropic)

The rest of this course uses Claude for its examples. If you're on Gemini or OpenRouter, follow along by changing the client setup and the call; the ideas (messages, roles, system prompts, history) are the same everywhere.

Install and set up

In Colab:

!pip install anthropic

import os
from google.colab import userdata
os.environ["ANTHROPIC_API_KEY"] = userdata.get("ANTHROPIC_API_KEY")

The call

import anthropic

client = anthropic.Anthropic()   # reads ANTHROPIC_API_KEY from the environment

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=16000,
    messages=[
        {"role": "user", "content": "Explain what a Python list is in two sentences."}
    ],
)

print(response.content[0].text)

Every part, explained

  • client is your connection to the API. Create it once and reuse it.
  • model chooses which model answers. Model names change as new versions come out; check the provider's models page for the current list.
  • max_tokens is the maximum length of the reply. It's a ceiling, not a target.
  • messages is a list of dictionaries: exactly the structure you learned in Lists, Dicts & Data. Each has a role ("user" or "assistant") and content.

What comes back

print(response.stop_reason)          # end_turn means it finished normally
print(response.usage.input_tokens)   # tokens you sent
print(response.usage.output_tokens)  # tokens it wrote

response.content is a list of blocks. For plain conversations the text is in a block of type "text". A safe way to collect it:

answer = "".join(b.text for b in response.content if b.type == "text")

Wrap it in a function

MODEL = "claude-opus-5-5"

def ask(question):
    response = client.messages.create(
        model=MODEL,
        max_tokens=16000,
        messages=[{"role": "user", "content": question}],
    )
    return "".join(b.text for b in response.content if b.type == "text")

print(ask("Give me 3 fun facts about Visakhapatnam port."))

Now asking AI is one line anywhere in your program, and the model name lives in one constant you can change.

Tip: Print response.usage while learning. Watching token counts teaches you quickly what makes a request cheap or expensive.

In one line

Create a client, call messages.create with a model, max_tokens and a list of messages, and read the text blocks from response.content.

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

Share with a friend