lesson
55 min

Building With LLMs Safely

Write structured prompts, call an LLM from Python, keep keys and personal data safe, and test outputs before trusting them.

Objective

By the end of this lesson, you can write a structured prompt, call an LLM API from Python with the key kept secret, and build a small test set to check its answers.

Prompts are specifications

When you build with an LLM, the prompt is part of your program. A good prompt states the role, the task, the context, and the exact output format. Vague prompts give vague, inconsistent results.

Prompt Lab

Write a prompt that summarizes a public notice in plain language for families, with a strict output format you can test.

Who should the AI act as?

What exactly should it do?

What does it need to know?

What shape should the answer take?

How should it sound?

Your prompt

Act as a plain-language writer for a community organization.

Format: Exactly three bullet points at a 6th-grade reading level, then one line: 'Check the original notice for dates and details.'

Tone: Calm and clear

Do not paste private details (full name, address, ID numbers) into any AI tool.

Call an LLM from Python

This needs the internet and an API key, so it will not run in the browser boxes on this site. Run it in Google Colab (store the key in Colab's Secrets panel) or on your own computer.

import os
from anthropic import Anthropic   # pip install anthropic (Colab: !pip install anthropic)

client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])  # never type the key into code

notice = "The public library will close at 1 p.m. on Friday for staff training."

message = client.messages.create(
    model="claude-sonnet-4-5",   # check the provider's docs for current model names
    max_tokens=300,
    messages=[{
        "role": "user",
        "content": "Summarize this notice in exactly three bullet points at a 6th-grade reading level:\n\n" + notice,
    }],
)
print(message.content[0].text)

Other providers work the same way: a client, a model name, and a list of messages. Model names change often. Always check the provider's current documentation.

Run-it-yourself steps

  1. Ask your instructor which provider and access your cohort uses, then get an API key from that provider's developer console.
  2. In Colab, open the key icon (Secrets) on the left, add ANTHROPIC_API_KEY, and turn on notebook access.
  3. Never paste the key into a cell, a chatbot, a screenshot, or a shared file.

Test before you trust

Treat an LLM like any other model: build a test set. Write 10 to 20 inputs where you know what a good answer looks like. Run them all. Check each output against simple rules.

An output checker

This JavaScript checks whether a model's output follows the format rules. Change the sample output so it breaks a rule, and watch the checks. (Keyboard users: press Esc, then Tab, to leave the editor.)

Loading the code editor...

Press Esc, then Tab, to move focus out of the editor.

Automatic checks catch format and obvious errors. A person still reads a sample of outputs for accuracy and tone.

Judgment check

LLM build rules

0 of 6 done

Go deeper: AI Training That Sticks: Teach the Real Task, Not the Tool

Ask. Answer. Build together.