Skip to content
mentoor

Topic 2 of 5 · 4 min read

Designing novel code before prompting

Goal: Identify when a task's design is novel enough that you should sketch it yourself before delegating any code.

When a task has no pattern to copy, decide the design yourself first, on paper, and only then ask an agent to type it. Models are strong at producing code that looks like code they have seen. They are weak at choosing between trade-offs they have never seen in your system.

Check yourself · 1 of 3

Which task most needs a human design sketch before prompting an agent?

Choose one answer

Why it matters

Ask an agent for a new billing model, a new sync protocol or a new permission scheme and it will give you something plausible. It will pick a data shape, names and boundaries without telling you it chose. Those choices spread into every later change, and they are costly to undo. A bad function is a ten-minute fix. A bad data model is a migration.

How to do it

  1. Ask: is there a pattern? If the repo already does this three times (a new REST endpoint, a new CRUD page), delegate and point at an example. If not, treat it as novel.
  2. Write the design in one page. The entities and their fields. The public interface: function signatures or endpoints. The invariants, meaning the facts that must always be true ("an invoice total equals the sum of its lines", "a user belongs to exactly one tenant").
  3. List what you rejected. Two alternatives and why not. This is what you check the agent against later.
  4. Then prompt for the parts. Give the agent the design as a spec. Ask for one piece at a time: the types first, then one function, then tests that encode the invariants.
  5. Check that the output kept your design. Agents quietly rename fields, add optional parameters and widen interfaces.

Before and after

Weak:

Add support for team workspaces so several users can share projects.

Better:

Design (already decided):
- Table workspace(id, name); table membership(workspace_id, user_id, role).
- role is one of owner | editor | viewer. Every workspace has at least one owner.
- Project.workspace_id replaces Project.user_id. No project exists without a workspace.
Task: write the migration and the Membership model only. Do not touch the API yet.
Tests must show that removing the last owner is rejected.

The second prompt moves the hard decisions out of the model's guesses and into your hands.

What goes wrong

  • Design by autocomplete. The first plausible schema becomes the schema.
  • Design in the prompt only. If the decisions live only in a chat, the next session forgets them. Save the design in a doc in the repo.
  • Over-designing. For a throwaway script, a page of design is more effort than the code. Match the effort to how hard the change is to undo.

Examples

Invariants as tests

The design says every workspace needs at least one owner. The test you ask the agent to write first:

def test_cannot_remove_last_owner(workspace, owner):
    with pytest.raises(LastOwnerError):
        workspace.remove_member(owner)

If the generated model fails this test, the agent has broken your design, not just your style.

Pattern or novel?

  • "Add a /teams/:id endpoint like /users/:id": a pattern exists. Delegate with the example file attached.
  • "Let two offline clients edit the same note and merge later": no pattern. Sketch the conflict rule yourself first.

Key points

  • Check for an existing pattern in the repo; if there is none, the task is novel.
  • Write entities, interfaces and invariants yourself before any code is generated.
  • Note the alternatives you rejected so you can spot an agent quietly drifting from the design.
  • Delegate in small pieces, with tests that encode the invariants.
  • Spend design effort in proportion to how costly the choice is to reverse.

Common mistakes

  • Asking an agent to design and implement a new subsystem in one prompt.
  • Keeping the design only in the chat history.
  • Skipping invariants, so nothing tells you the generated code is wrong.
  • Designing at length for work that is easy to throw away.

Check yourself · 2 of 3

What is an invariant in a design sketch?

Choose one answer

Check yourself · 3 of 3

You prompt an agent to design and build a new permissions system in one go. What is the main risk?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic