Prompting Fundamentals for Code: Prescriptive, Outcome-Based and Guided Prompts
Ways of asking an AI tool for code, from prescriptive (you decide everything) to outcome-based (the AI decides how), plus refinement, few-shot and role prompts, golden rules, and a calculator prompt read against the code it produced.
What you will learn
- Pick the right place to prompt: a chat app, AI inside the IDE, or a terminal agent
- Write prescriptive, outcome-based and guided prompts for the same coding job
- Use iterative refinement, few-shot examples and a role line
- Turn a bad calculator prompt into a Role, Context, Task, Format prompt and check the code against it
Before you start
- Your First Python Program
- Run a Python File and Read the Error
Imagine you ask a friend to bake you a cake, and all you say is "make a cake". Now they have to guess: what flavour, how big, for when? So you write a better note: chocolate, for eight people, no nuts, ready by six. Each guess turns into something they actually know.
That note is a prompt. A prompt is simply what you tell an AI tool, and like your friend, it only knows what you tell it. You'll ask AI tools for help with code all the time, and what you get back depends a lot on what you ask.
Three places to prompt
- A chat app, like ChatGPT, Claude or Gemini. You prompt, copy the code, paste it into your file and run it. Fine for a snippet or a file or two, but it can't see your project, so it makes more mistakes as the program grows.
- AI inside the IDE, like the AI chat in PyCharm, or Cursor. It can see your files, so it knows what your application is about and the answers fit much better.
- A terminal agent, like Claude Code or Codex. You give it a job in the terminal and it works across your files step by step.
Pick the tool for the job. A chat app is handy for a quick question, but it isn't the place to build a full application. For coding, start in your IDE and get comfortable there.
Ways of asking
Prescriptive: you decide everything
Write a Python function called calculate_average. It takes a list of integers. Use a for loop to add them up, then divide by the length. Return a float, rounded to two decimal places. If the list is empty, return 0.0. Add type hints and a docstring.
That fixes the name, the input, the method, the return type and the empty-list case. Type hints and a docstring are notes in the code that say what a function takes and what it does. Use this style when you need a specific algorithm or pattern, or you have a strict requirement. You'll get close to exactly what you asked for.
Outcome-based: say what, not how
I need a function that calculates the average of a list of numbers. It should handle edge cases gracefully.
No function name, no loop, no rounding. The how is left to the AI, and sometimes it picks a neater approach than the one you had in mind. If the requirement is strict, go prescriptive. If you're open to ideas, give the outcome.
Guided or hybrid: a bit of both
Build a student grade tracker. Store names and scores in a dictionary, and print a summary using f-strings. You decide the exact structure and helper functions.
The dictionary and the f-strings are fixed; the structure stays open. You hold on to what matters to you and leave the rest free.
Iterative refinement
This is the one you'll use most. Prompt one: "Write a to-do list app in Python." Read what comes back and see what you still want. Prompt two: "Now add a priority field to each task." Read again. Prompt three: "Add file persistence using JSON, so the tasks get saved." You'll rarely get exactly what you want the first time, and that's normal.
Few-shot and role-based
Few-shot means you show examples. "Convert these names to slugs", then one pair: Hello World becomes hello-world. A slug is the small, dashed version of a name you see in web addresses. Now the AI can see the pattern instead of guessing it. There's much more on this in Few-shot prompting done right.
Role-based means you tell the AI who to be: "You are a senior Python developer who follows PEP 8" (Python's style guide). It changes how the answer is written, and it only takes one line. You can mix all of these: a role, some examples, then a few rounds of refinement.
One framework: Role, Context, Task, Format
- Role: who should the AI be?
- Context: what does it need to know about you and your project? The language, the framework, the version.
- Task: what exactly should it do?
- Format: what should the answer look like? Only code, a list, a table?
The four parts are taken apart in detail in Anatomy of a good prompt.
Golden rules
- Be specific. A vague prompt gets a vague answer.
- Provide context: the language, the framework, the version.
- One task at a time. Ask for a whole social network in one prompt and it breaks in many places you can't debug. Break it into steps.
- Include examples of the input and output you expect.
- State your constraints, like no external libraries. If your server has no GPU and you never say so, the code may need one and crash when you deploy it.
- Ask for explanations, and ask for the plan first. If you like the plan, go ahead; if not, ask it to improve the plan.
From bad to better: a calculator
A bad prompt: "Write me some code for a calculator." What kind? What should it do? It's all guesswork. Now build it up with the framework:
You are a Python teacher writing for beginners. I'm new to Python, it's one file, and no external libraries. Write calculator.py with add, subtract, multiply and divide functions. If b is zero, divide returns the text "Cannot divide by zero". Code only, and end with five print lines: each function with 8 and 4, then divide 8 by 0.
Here's one example of code that fits this prompt. An AI tool might write it a little differently.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Cannot divide by zero"
return a / b
print(add(8, 4))
print(subtract(8, 4))
print(multiply(8, 4))
print(divide(8, 4))
print(divide(8, 0))
Read it against the prompt
The task asked for four functions, and you can point to them. Each def is a small named job; functions come properly in Functions, scope, args and kwargs. The check for b being zero is there, two lines inside divide. The context said one file and no external libraries: there's no import line. The format asked for five print lines, and they're at the bottom.
Do this every time. Read what you're given and compare it to what you asked for, because AI can get things wrong.
Now run it
12
4
32
2.0
Cannot divide by zero
Division in Python gives a float, so it's 2.0, not 2. You might not have guessed that from reading alone, which is why you run it. And the last line is the case you asked for: zero goes into b, the check is true, the text comes back, and the a / b line never runs. One line in the prompt became a check in the code and a line in the output.
Do's and don'ts
| Do | Don't |
|---|---|
| name the language and version, and the edge cases you care about | copy and paste without reading |
| ask for tests alongside the code | assume AI is always right |
| paste the full error message when something breaks | ask five things in one huge prompt; long chats lose earlier details |
| use follow-up prompts and review what comes back | put passwords or API keys in a prompt |
| learn the concepts | skip them; they're how you know an answer is any good |
Modes, agents and voice
The mode names change from tool to tool. Deep think spends longer reasoning on a hard problem. Deep research searches the web and documents. Plan mode works out a plan before writing code. Multi-step agent mode plans, writes, tests and fixes code across several files; that's what people mean by an agent. Junie from JetBrains, Claude Code from Anthropic and Codex from OpenAI are examples. The tools keep changing, so don't worry about which one is best. A clear prompt helps in all of them, and you still check the work.
You can also prompt by voice. Many chat apps have a microphone button, and Windows has dictation on Windows key + H. Speak in complete sentences, use a quiet room and a good microphone, spell out technical words, and give exact examples. Voice loses punctuation and formatting, so read the text before you send it.
Quick recap
| Technique | Use it when |
|---|---|
| Prescriptive | you have a strict requirement or a specific method |
| Outcome-based | you're open to the AI's approach |
| Guided / hybrid | some parts matter to you, the rest can be free |
| Iterative refinement | almost always |
| Few-shot | a pattern is easier to show than to describe |
| Role | you want answers written from a point of view |
Try it yourself
Take this bad prompt, "make a shopping list program", and rewrite it with Role, Context, Task and Format. Then check what comes back against each line you wrote, as in Checking AI-written Python code.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
The AI Stack in One Picture: Six Layers Every AI Product Is Built From
Every AI product is built from six layers: data, model, retrieval, orchestration, serving and ops, with evals and security across all of them. A restaurant analogy, one request traced through all six, and what an AI engineer actually builds around the model call.
Tokenization: What an LLM Actually Sees When You Type
A language model never sees letters, only token ids. Use tiktoken to watch strawberry become three ids, see a space or a capital change them, fix the letter-counting problem, and compare English, German and Hindi token counts.
LLM Tokens, Context Windows and Cost: What One Question Really Costs
Follow one four-word help desk question that becomes 911 input tokens. Learn what fills the context window, why chat memory grows every turn, how max_tokens cuts a reply, and how to price a call.
What an LLM Can and Cannot Do: One Prediction Loop, Its Strengths and Its Gaps
A large language model predicts the next token, over and over. That one loop makes it strong at drafting, summarising and classifying, and weak at fresh facts, exact maths, memory and guaranteed truth. See why with a three-line tiktoken script, and learn the fix for each gap.
