Build an AI in 10 Lines of Code
We're going to build a tiny command-line app that accepts a prompt and returns a model response. The point is not that this is a full product. The point is that the OpenAI Python SDK makes a useful baseline demo very small.
Create an API key in the OpenAI dashboard and export it as OPENAI_API_KEY before running the example.
Store the key in your shell profile or another secure location, not in the source file.
Set up a new Python project:
mkdir openai-ama
cd openai-ama
python3 -m venv .venv
source .venv/bin/activate
pip install openai
Now create a file called ama.py.
The script will do three things:
- read user input,
- send that input to the Responses API,
- print the model's reply.
from openai import OpenAI
client = OpenAI()
while True:
prompt = input("Ask OpenAI Anything (or type 'exit'): ")
if prompt.strip().lower() == "exit":
break
response = client.responses.create(
model="gpt-4.1-mini",
input=prompt,
max_output_tokens=200,
)
print(response.output_text)
A few things are worth calling out:
- The modern SDK uses
OpenAI()from theopenaipackage. - The Responses API is the recommended path for new projects.
response.output_textgives you the text answer directly, which keeps the demo simple.max_output_tokenscontrols how much the model can generate in a single response.
From here, you can turn the same loop into a more useful tool:
- a writing assistant,
- a brainstorming helper,
- a lightweight support bot,
- or a command-line wrapper around a larger workflow.
The shape stays the same. You take input, send it to the model, and use the response where it matters.