Stop trusting AI to write your final commit because LLMs are

PromptCube Novice 19h ago 320 views 12 likes 2 min read

The Prompting Framework for Robust Code

To get code that doesn't break under edge cases, you need to constrain the AI. Instead of asking "Write a function to handle user uploads," you need to provide a strict technical context. I've found that specifying the error handling strategy and the exact data shapes prevents 80% of the typical AI hallucinations.

Try structuring your request like this:
1. Define the Interface: Explicitly state the input types and the expected return object.
2. Constraint Mapping: List the "must-nots" (e.g., "do not use external libraries for this," "ensure O(n) time complexity").
3. Edge Case Definition: Tell the AI exactly how to handle nulls, timeouts, or malformed JSON.

A Practical AI Workflow for Deployment

The secret to a real-world AI workflow isn't the prompt—it's the iterative loop. I treat the AI as a junior developer who is incredibly fast but occasionally delusional.

First, use the AI to scaffold the logic. Then, immediately prompt it to act as a senior reviewer. I usually run a second pass with a prompt like:

Review the code provided above. Identify three potential memory leaks or race conditions that could occur under high concurrency and rewrite the function to mitigate them.

Moving from Snippets to Systems

The biggest mistake beginners make is treating AI as a snippet generator. Production code is about how components interact. When I'm building a full feature, I use a "Context-First" approach. I feed the AI the existing API signatures or the database schema before asking for the logic. This ensures the generated code actually fits into the existing codebase without requiring a massive refactor.

For those doing a deep dive into prompt engineering for coding, remember that the AI doesn't know your infrastructure. If you're deploying to a serverless environment with a 30-second timeout, tell the AI. If you're targeting a specific version of Python or Node.js, specify it.

  • Drafting: Use LLMs for the heavy lifting of boilerplate.
  • Verification: Use a separate prompt for unit test generation.
  • Refinement: Manually review the logic for architectural fit.

If you want to move from "it works on my machine" to "it works in production," stop treating the AI as an oracle and start treating it as a tool that needs a strict set of boundaries. The quality of the output is always a direct reflection of the constraints you provide.
cursorClaude Codepytest

All Replies (4)

S
SoloSage Advanced 19h ago
I usually feed it my existing linting rules first so it stops hallucinating syntax.
0 Reply
A
Alex17 Advanced 19h ago
Does this approach work better if you provide the specific TS types upfront?
0 Reply
G
GhostGeek Expert 19h ago
Just a heads up, I'm ditching Reddit and moving back here. The quality has just tanked—check out this thread to see what I mean: https://www.reddit.com/r/ClaudeAI/comments/1vj72hj/how_to_wr...
0 Reply
J
JulesTinkerer Intermediate 19h ago
@GhostGeek Welcome back! Honestly, the discussions here feel way more genuine lately. Glad you're making the switch.
0 Reply

Write a Reply

Markdown supported