Article
Talk to an AI from Python in about 20 lines
Send a prompt to an AI model from Python and print the reply — a short, beginner-friendly walkthrough with almost no setup drama.

You do not need a big “AI engineering” course to try this.
In this article you will write a tiny Python program that:
- Sends a short question to an AI model
- Gets a text answer back
- Prints it in your terminal
We keep the language simple. If a word is new, we explain it in one line.
What is an API? (10-second version)
An API is a way for your program to talk to another service on the internet.
You send a message (your prompt).
The service sends a message back (the model’s reply).
What you need
- Python 3
- An API key from a provider that lets you call a chat model
(OpenAI is common; other hosts work the same idea) - The
openaiPython package
Install the package:
pip install openai
Keep your secret key safe
Your API key is like a password. Do not paste it into GitHub.
On Windows PowerShell (current window only):
$env:OPENAI_API_KEY = "paste-your-key-here"
On macOS / Linux:
export OPENAI_API_KEY="paste-your-key-here"
The whole program (read it once)
Save as ask_ai.py:
import os
from openai import OpenAI
# 1) Create a client (reads OPENAI_API_KEY from the environment)
client = OpenAI()
# 2) Your question
question = "Explain a Python list in one short paragraph for a beginner."
# 3) Call the model
response = client.chat.completions.create(
model="gpt-4o-mini", # a small, cheap chat model — change if your account uses another name
messages=[
{
"role": "system",
"content": "You explain coding simply. No jargon unless you define it.",
},
{
"role": "user",
"content": question,
},
],
)
# 4) Pull out the text and print it
answer = response.choices[0].message.content
print(answer)
Run:
python ask_ai.py
You should see a short explanation in plain English.
What each part means
OpenAI()
Builds a helper object that knows how to send requests. It looks for OPENAI_API_KEY automatically.
messages
Chat models work like a short conversation:
- system — rules for how the AI should behave
- user — your actual question
You can add more user/assistant turns later. For day one, two messages is enough.
response.choices[0].message.content
The service can return more than one “choice.” We take the first one and read its text.
Make it interactive (still short)
Ask from the keyboard instead of a fixed string:
import os
from openai import OpenAI
client = OpenAI()
question = input("Ask the AI something: ").strip()
if not question:
print("You typed nothing — exiting.")
raise SystemExit
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Reply in clear, simple English."},
{"role": "user", "content": question},
],
)
print("\n--- AI ---")
print(response.choices[0].message.content)
Tiny upgrades you can try
1. Shorter answers
Add:
max_tokens=120,
inside create(...) so replies stay short.
2. More focused help
Change the system line to:
"You are a patient Python tutor. Use short sentences and one example."
3. Save the answer to a file
from pathlib import Path
Path("answer.txt").write_text(answer or "", encoding="utf-8")
print("Saved to answer.txt")
If something goes wrong
| Message / problem | Likely fix |
|---|---|
| Missing API key | Set OPENAI_API_KEY in the same terminal, then run again |
| Model not found | Your account may use a different model name — check the provider docs |
| Rate limit / quota | Wait a minute, or check billing / free-tier limits |
| Network error | Check wifi; try again |
What you learned
- How a program calls an AI over the internet
- How to send a system instruction + user question
- How to print (or save) the reply
That is the core loop behind chat apps, study tutors, and many “AI features” — including teaching tools like Sythra’s AI tutor idea: the model helps, but you still drive the learning.
Practice idea
Ask the model to:
- Explain
forloops simply - Then ask it to give a 5-line practice exercise
- Solve the exercise yourself in Python — do not paste the answer back until you tried
Want structured labs around Python and ML? Open app.sythra.ai when you are ready for guided practice.
Twenty lines is enough to start. Curiosity does the rest.

