DEV Community

Kai Thorne
Kai Thorne

Posted on

The Prompt Engineering Framework That Cuts My Coding Time in Half

The Prompt Engineering Framework That Cuts My Coding Time in Half

I used to think prompt engineering was just "ask the AI nicely." Type your request, get code, paste it in. If it doesn't work, try again until it does.

That approach took me 4 hours to build what should've taken 30 minutes. I was generating broken code, debugging AI hallucinations, and rewriting more than I was writing myself.

Then I developed a 4-part framework that changed everything. Now I build full features in a fraction of the time — and the code compiles on the first try more often than not.

Here's the exact system I use.


The 4-Part Prompt Framework

Every prompt I write follows this structure:

CONTEXT — What the project is and who it's for
CONSTRAINTS — What the code must and must not do
FORMAT — How I want the output structured
EXAMPLES — Reference patterns the AI should follow
Enter fullscreen mode Exit fullscreen mode

Let me show you each piece with real code.


1. CONTEXT: Set the Stage

Bad prompt:

"Write a cron job that checks my Etsy API and emails me sales data."
Enter fullscreen mode Exit fullscreen mode

This is too vague. The AI doesn't know:

  • What language you're using
  • What Etsy API endpoints exist
  • Whether you use a local mail server or SendGrid
  • How often the cron should run

Better prompt:

"I have a Node.js side project that runs on a Linux VPS (Ubuntu 22.04, Node 20).
I use SQLite via better-sqlite3 for local data storage and nodemailer for email.
The project already has a config.json with ETSY_API_KEY and ETSY_SHOP_ID set.

I need to extend it with a cron job that:
- Runs daily at 8 AM UTC
- Queries the Etsy v3 API for today's orders
- Writes each order into a local SQLite `orders` table
- Emails me a summary if there are 3+ new orders"
Enter fullscreen mode Exit fullscreen mode

With that context, the AI generates production-ready code on the first shot — because it knows the exact environment, tooling, and data flow. No guessing, no "oh, I should've mentioned I use Node."

The rule: Include your stack, your constraints, and your existing infrastructure in every prompt. It takes 30 extra seconds and saves 30 minutes of debugging.


2. CONSTRAINTS: Draw the Fences

AI models default to the most generic solution. If you don't tell them what NOT to do, they'll pick dependencies you don't want, use patterns that don't fit your architecture, and write code that works in isolation but breaks in production.

Example constraint block from a recent prompt:

CONSTRAINTS:
— Zero new dependencies. Use Node.js built-in modules only (fs, path, https, crypto).
— No external API wrappers — call the REST API directly with https.get.
— All error handling must write to a local error.log with timestamps.
— No try/catch that swallows errors silently.
— Functions must be < 40 lines each.
— Use CommonJS (require/module.exports), not ESM.
Enter fullscreen mode Exit fullscreen mode

By explicitly saying "zero new dependencies," I eliminate whole categories of bad output. The AI can't suggest npm packages. It can't reach for express or axios. It has to write raw, self-contained code that fits my no-bloat philosophy.

The rule: Block bad outcomes before they happen. Say what you DON'T want as clearly as what you DO want.


3. FORMAT: Shape the Output

If I ask for "a script that does X," most AI models dump 200 lines of uncommented code in a single markdown block. That's useless for a real project.

Specify the output format upfront:

FORMAT:
— Return the code in 3 files: lib/api.js, lib/db.js, index.js
— Each file must have a JSDoc header with purpose, author, and last modified
— Functions must have JSDoc type annotations
— Include a USAGE comment block at the top showing how to run it
— Export a --dry-run flag for testing without side effects
Enter fullscreen mode Exit fullscreen mode

Now instead of one monolithic blob, I get a well-structured project I can drop into my codebase immediately. The AI models I use (GPT-4, Claude, DeepSeek) all respect format instructions — you just have to give them.

The rule: Tell the AI HOW to give you the answer, not just WHAT the answer is. File structure, naming conventions, documentation style — spell it all out.


4. EXAMPLES: Show, Don't Just Tell

This is the most powerful part. A single example of the pattern you want is worth 500 words of instruction.

I maintain a prompt-examples/ directory in every project with 2-3 files showing the code style I want:

// Example pattern: how I write API wrappers
const https = require('https');

async function etsyFetch(endpoint, method = 'GET') {
  return new Promise((resolve, reject) => {
    const url = new URL(endpoint, 'https://openapi.etsy.com/v3');
    const options = {
      method,
      headers: {
        'x-api-key': config.ETSY_API_KEY,
        'Accept': 'application/json'
      }
    };
    const req = https.request(url, options, (res) => {
      let data = '';
      res.on('data', chunk => data += chunk);
      res.on('end', () => resolve(JSON.parse(data)));
    });
    req.on('error', reject);
    req.end();
  });
}
Enter fullscreen mode Exit fullscreen mode

When I include this pattern in my prompt, the AI generates sibling functions that match the style — same error handling, same https pattern, same naming conventions. It's like giving the AI a style guide it can't ignore.

The rule: Always include 1-2 reference code blocks in your prompts. The AI will mirror the style, structure, and conventions automatically.


Putting It All Together: A Complete Prompt

Here's what the framework looks like in a real prompt. This is an actual prompt I used last week to generate a Gumroad revenue tracker:


CONTEXT: Node.js 20 project on Ubuntu 22.04. Uses SQLite (better-sqlite3) and nodemailer. Existing codebase has config.json with API keys.

TASK: Build a daily revenue sync script that:

  1. Fetches yesterday's sales from Gumroad API
  2. Inserts/updates records in a daily_revenue SQLite table
  3. Flags any day where revenue dropped > 50% compared to 7-day average
  4. Sends a Telegram alert (via https.get to api.telegram.org) when flagged

CONSTRAINTS:
— No new npm packages. Use built-in https only.
— All functions < 30 lines.
— Write errors to error.log with ISO timestamps.
— Use async/await, no raw promises.
— CommonJS modules only.

FORMAT:
— 1 file: lib/gumroad-sync.js
— JSDoc on all exported functions
— CLI entry point in index.js with --dry-run flag
— console.table output when --dry-run is used

EXAMPLES:

// This is the existing etsyFetch pattern — match this style
async function fetch(endpoint) {
  return new Promise((resolve, reject) => {
    const url = new URL(endpoint, 'https://api.gumroad.com/v2');
    const { hostname, pathname, search } = url;
    const opts = {
      hostname, path: pathname + search,
      headers: { Authorization: `Bearer ${config.GUMROAD_TOKEN}` }
    };
    https.get(opts, res => {
      let d = '';
      res.on('data', c => d += c);
      res.on('end', () => resolve(JSON.parse(d)));
    }).on('error', reject);
  });
}
Enter fullscreen mode Exit fullscreen mode

Result: I got a complete, working sync script in one shot. No tweaks. No follow-up prompts. The code matched my existing project's style, used the same error-handling pattern, and even included the dry-run flag I asked for.

Before this framework, that would've been 3-4 iterations minimum.


Why This Works

The framework works because it addresses the four failure modes of AI-generated code:

Failure Mode Framework Fix
AI doesn't know your stack CONTEXT tells it
AI picks wrong dependencies CONSTRAINTS block them
AI returns messy output FORMAT structures it
AI ignores your code style EXAMPLES demonstrate it

Each piece is simple. Together, they transform AI from a guessing machine into a reliable engineering partner.


Your Cheat Sheet

Save this template. Use it for every AI coding prompt:

CONTEXT: [stack, environment, existing infrastructure]
TASK: [what needs to be built, in detail]
CONSTRAINTS:
— [must-haves and must-not-haves]
FORMAT:
— [file structure, naming, documentation]
EXAMPLES:
[code blocks showing your preferred style]
Enter fullscreen mode Exit fullscreen mode

I use this for everything now — API wrappers, SQL queries, cron jobs, even this blog post. It's the single highest-leverage change I've made to how I work with AI.


Want 50 ready-to-use prompt templates like this one? I've packaged my best prompts into a downloadable pack — including templates for API integrations, database migrations, cron jobs, and full-stack features. Check it out on Gumroad →

Follow me on dev.to for more practical AI and side-hustle content. I post weekly with code you can actually use.


📦 Get the AI Coding Assistant Prompt Library — 50 battle-tested prompt templates for developers. API integrations, database migrations, cron jobs, and full-stack features. Every template uses the CONTEXT → CONSTRAINTS → FORMAT → EXAMPLES framework from this post.

👉 Download on Gumroad — $7

Top comments (0)