Deep Agents/Agentic Coding
Intermediate8 min

Prompt Engineering for Code Generation

Code generation prompting is different from general prompting: specify the language, framework, and style; provide context via file references; request tests alongside implementation; and constrain the output format.

Quick Reference

  • Always specify: language, framework version, coding style, and error handling approach
  • Provide context: relevant file contents, type definitions, existing patterns to follow
  • Request tests: ask for implementation AND tests in the same prompt — higher quality code
  • Constrain output: 'only modify the function X' prevents unwanted changes elsewhere
  • Few-shot: show one correct example, then ask for more — models follow patterns reliably
  • Diff format: for edits, ask for the specific changes rather than the entire file

General Prompting vs Code Prompting

AspectGeneral PromptingCode Prompting
SpecificityModerate — natural language is flexibleVery high — wrong import path = broken code
ContextConversation historyFile contents, type definitions, APIs, dependencies
ValidationSubjective qualityObjective — does it compile? Do tests pass?
FormatProse, bullets, tablesCode blocks with exact syntax
Error costLow — rephrase and try againHigh — wrong code can break production