Tutorial 2: Prompt for Code
- Contributor
- 5 days ago
- 2 min read
Vague prompt → vague code. Specific input gets specific output.
Step 1: State the Goal (10 min)
Bad: "Write a sorting function."
Good: "Write a TypeScript function that sorts users by lastName,
then firstName, both case-insensitive."
The model can't read your mind. Specify:
Language
Inputs
Outputs
Constraints
Step 2: Include Context (10 min)
class User(BaseModel):
id: int
email: EmailStr
Tell the model: "We use Pydantic models for validation. Write a similar model for Order with fields: id (int), user_id (int), amount (Decimal, > 0), status (enum: pending/paid/cancelled)."
Models match local conventions when shown them. Less rework.
Step 3: Be Explicit About Constraints (10 min)
Function must:
- Run in < 100ms for 10k items
- Handle empty list gracefully
- Not mutate the input
- Use only stdlib
Constraints up front. Model designs to them.
Without: model picks freely; you fix later.
Step 4: Show, Don't Just Tell (10 min)
Input:
[{name: 'Alice', age: 30}, {name: 'Bob', age: 25}]
Expected output:
[{name: 'Bob', age: 25}, {name: 'Alice', age: 30}]
Function: sort by age ascending.
Example output anchors the implementation. Fewer "almost right" cases.
Step 5: Iterate, Don't Restart (10 min)
After first response:
That's close. Change:
1. Use type hints (TypeScript)
2. Handle null inputs
3. Make case-insensitive
Don't rewrite the prompt. Iterate.
The model carries context. Use it.
Step 6: Ask for Tests (10 min)
Write the function and 5 test cases covering:
- Empty input
- Single item
- Duplicates
- Pre-sorted
- Reverse-sorted
Tests describe behavior. Often: writing tests via AI reveals what the implementation should do.
Then you verify the tests yourself.
Step 7: Specify the Output Format (5 min)
Return:
1. The function code
2. Tests as a separate code block
3. Brief explanation
Without: messy mixed output.
For structured tasks, even more specific: "Output as YAML matching this schema: ..."
Step 8: Use the Right Persona (5 min)
Act as a senior Python engineer who prioritizes simplicity over
cleverness.
Personas nudge style. Mileage varies.
Often: "expert in X" with adjectives matters more than the persona itself.
Step 9: Don't Skip the Why (10 min)
I'm building a high-traffic API; this function runs on every
request. Need it as fast as possible. Memory matters less.
Context → better implementation choices.
Without: model defaults to readability over performance.
Step 10: Verify (10 min)
The output looks right. Test it:
Run the code
Check edge cases
Read it carefully
LLMs make confident wrong code. Always verify.
Especially security-sensitive code: read every line.
What You Just Did
Prompting for code: goal, context, constraints, examples, iterate, tests, format, persona, why, verify. Quality prompts.
Common Failure Modes
Vague prompt. Vague code.
No context. Code that ignores conventions.
Don't iterate; rewrite. Lose carried context.
Skip verification. Confident-wrong code ships.
Over-specified prompt. Brittle to small changes.


