Relay365Multi-model API relay desk
← All desk bulletins
Field notes from the deskEN159 desk reads

Better Code Generation Prompts, Step by Step

A good code generation prompt is a small engineering brief. It tells the assistant what to build, where the code belongs, and how you will judge the result. You do not need special phrases or a long conversation to begin. Follow this walkthrough to turn a vague request into a practical, testable assignment.

Describe one observable coding outcome

1. Start with a single task whose result you can check. Instead of asking for an expense management application, ask for a function that summarizes expenses by category. Smaller requests make misunderstandings easier to spot before they spread through a larger implementation.

For example, write: “Create a Python function named summarize_expenses that accepts CSV text and returns the total amount for each category.” This establishes the language, interface, and outcome without prescribing every implementation detail.

Supply the environment and relevant existing code

2. Explain where the generated code will run. Include your language version, permitted dependencies, and any existing function signature or project conventions. If you are changing a repository, provide the relevant files or let your coding assistant inspect them before editing.

For our example, add: “Use Python 3.12 and only the standard library. This is a standalone utility, not a web service. Accept the CSV content as a string; do not read files or make network requests.” These details prevent unnecessary frameworks and infrastructure.

Make input rules and failure behavior explicit

3. Define the boundary cases that matter. An assistant cannot reliably infer whether missing values should trigger errors, become zero, or disappear from the results. State the decision rather than leaving it to chance.

  • The CSV must contain category and amount headers, and extra columns may be ignored.
  • Category names must have surrounding whitespace removed and must not be empty.
  • Amounts must use decimal arithmetic, and negative amounts represent refunds.
  • Invalid rows must raise ValueError with the CSV record number, counting the header as record one.
  • A header-only CSV must return an empty dictionary, while missing required headers must raise ValueError.

Keep requirements relevant to the task. An exhaustive specification for unrelated future features only adds distractions.

Show examples that establish correct behavior

4. Add a small example with an expected result. Say that two Food entries with amounts 0.10 and 0.20 must produce a Food total of Decimal('0.30'). Add a negative entry to demonstrate how refunds affect the total.

Then request tests for repeated categories, whitespace, invalid amounts, missing headers, and header-only input. Examples make your intent concrete, while tests give you something executable to inspect. Neither replaces checking whether the requirements themselves make sense.

Specify a response you can review quickly

5. Tell the assistant what to return. For this task, request the function, standard-library unit tests, and a short explanation of assumptions. Ask it to identify unresolved requirements rather than silently inventing business rules.

If it is editing existing code, narrow the scope: “Change only the expense summary utility and its tests. Preserve unrelated behavior. Explain any new dependency before adding it.” Review stays manageable when the response matches the size of the assignment.

Use test results to guide the next prompt

6. Run the generated tests and examine the implementation before using it. Check that the code actually uses decimal arithmetic, reports the intended record numbers, and follows your input rules. Treat generated code as a proposed change, not a verified deliverable.

When something fails, provide the failing input, expected result, actual result, and relevant error message. Ask for a targeted correction that preserves passing behavior. “Fix the missing-header case without changing the public function signature” is more useful than “try again.”

Compare coding workflows without changing the assignment

7. Once your prompt works, save it as a reusable brief. If you compare models, keep the requirements and tests unchanged so you can evaluate the resulting code against the same standard.

Relay365 can fit this workflow when you want a pay-as-you-go relay desk for multiple model families. It works with Claude Code, Codex, Gemini CLI, and OpenAI SDK clients. Create a Relay365 account if that setup suits your workflow, and start with one small, testable task. The strongest habit is not writing longer prompts; it is making success easier to verify.

Further desk bulletins

Curious how it runs on your own code? Sign up →