How to Intermediate · 3 min read

Prompting techniques for code generation models

Quick answer
Use clear, explicit instructions with context and examples when prompting code generation models like gpt-4o or claude-3-5-sonnet-20241022. Include input-output examples, specify language and style, and request step-by-step explanations to improve output quality.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable for secure access.

bash
pip install openai>=1.0

Step by step

Use explicit prompts with clear instructions, specify the programming language, and provide input-output examples to guide the model. Below is a complete example using gpt-4o to generate a Python function that reverses a string.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

prompt = (
    "Write a Python function named <code>reverse_string</code> that takes a string and returns it reversed. "
    "Example:\nInput: 'hello'\nOutput: 'olleh'\n"  
    "Provide only the function code without extra text."
)

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

print(response.choices[0].message.content)
output
def reverse_string(s):
    return s[::-1]

Common variations

You can use streaming for real-time code generation, switch to claude-3-5-sonnet-20241022 for improved coding benchmarks, or add step-by-step reasoning in the prompt for complex tasks.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = "You are a helpful coding assistant."
user_prompt = (
    "Write a JavaScript function <code>factorial</code> that returns the factorial of a number. "
    "Explain your steps briefly in comments."
)

message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=256,
    system=system_prompt,
    messages=[{"role": "user", "content": user_prompt}]
)

print(message.content[0].text)
output
function factorial(n) {
  // Base case: factorial of 0 is 1
  if (n === 0) return 1;
  // Recursive case
  return n * factorial(n - 1);
}

Troubleshooting

  • If the model returns incomplete code, increase max_tokens or use streaming.
  • If output is off-topic, clarify instructions and add examples.
  • For syntax errors, specify the language and request code only.

Key Takeaways

  • Always specify the programming language and desired output format in your prompt.
  • Provide input-output examples to guide the model’s code generation.
  • Use step-by-step instructions or comments for complex code tasks.
  • Leverage streaming or increase token limits for longer code outputs.
  • Clarify instructions to reduce hallucinations and syntax errors.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗