0Pricing
NLP Academy · Lesson

Calling an LLM From Python

Send prompts and parse responses.

Calling an LLM From Python is a free NLP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Talk to a Model in Code

You do not run giant models on your laptop. Instead you send text to a hosted model over an API and get a reply back. 🌐

Install a Client

Most providers ship a Python client library so you can call the model with a few clean lines instead of raw HTTP.

pip install openai

Keep Your Key Secret

Calls are authenticated with an API key. Store it in an environment variable, never hard-coded in your source files.

import os
key = os.environ["OPENAI_API_KEY"]

Create the Client

You start by building a client object. It reads your key and handles the network details for every request you make.

from openai import OpenAI
client = OpenAI()

Messages, Not Just Text

Chat models take a list of messages, each tagged with a role like system, user, or assistant.

The System Role

A system message sets the model's behavior up front, like telling it to answer briefly or act as a helpful tutor.

Send Your Prompt

You put your question in a user message and send the whole list to the model in a single call.

resp = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "user", "content": "Hi!"}])

Parse the Response

The reply is a structured object. The text you want sits inside the first choice, ready to read or store.

text = resp.choices[0].message.content
print(text)

Control With Temperature

The temperature setting controls randomness. Low values give steady answers; high values give more creative, varied ones.

Cap the Output

Setting max tokens limits how long the reply can be, which keeps responses tidy and your costs predictable.

Handle Failures

Networks fail and limits get hit, so wrap calls in try/except and retry gracefully when an error comes back.

Quick Check

Where do you find the model's text in a chat completion response?

Recap

Install a client, load your key from the environment, send role-tagged messages, then read the text from the first choice. ✅

Frequently asked questions

Is the “Calling an LLM From Python” lesson free?

Yes — the full text of “Calling an LLM From Python” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Calling an LLM From Python”?

Send prompts and parse responses. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start NLP Academy?

No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Calling an LLM From Python” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this NLP Academy lesson?

Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Makes a Model Large
  2. Calling an LLM From Python
  3. Zero-Shot and Few-Shot Prompting
  4. Structured Output and Guardrails
← Back to NLP Academy