Why AI Code Generation Fails: The Communication Problem
Source: Dev.to
Introduction
“I asked ChatGPT to make a login feature, but it gave me weird code.”
I hear this complaint a lot lately. AI tools (GitHub Copilot, ChatGPT, Claude, …) are everywhere, yet the output often isn’t production‑ready.
Is AI the problem? No.
The problem is how we communicate with AI.
A Real‑World Example
Prompt
Make a login feature
AI’s Response
def login(username, password):
if username == "admin" and password == "password":
return {"status": "success"}
return {"status": "failed"}
“What is this? AI is not good…”
Who made the mistake?
If you tell a junior developer (or an AI) only “make a login feature”, you’ll get follow‑up questions such as:
- Which framework should I use?
- Which database?
- Session‑based or JWT?
- Do we need social login?
- Any password rules?
AI doesn’t ask those questions—it just gives the most generic answer it can.
“Garbage In, Garbage Out” → “Ambiguous Request In, Ambiguous Code Out”
| Request | Result | Practicality |
|---|---|---|
| “Make a user‑management feature” | Basic CRUD skeleton (≈30 lines) | 10 % |
| “Make a user‑management API with Django REST Framework - Email authentication - JWT token - Permission groups (admin, user) - Profile image upload - Soft delete - Pagination” | High‑quality code (≈500 lines) | 85 % |
The difference is the amount of context you provide.
What AI Needs to Produce Usable Code
1. Background
- Project type (e.g., e‑commerce, SaaS)
- Technology stack (frameworks, language versions)
- Constraints (performance, security, compliance)
2. Specific Requirements
- Feature list (what exactly should be built)
- Non‑functional requirements (latency, scalability)
- Edge cases (failure modes, validation rules)
3. Examples or References
- Similar code snippets
- API documentation links
- Desired output format (JSON schema, OpenAPI spec)
Bad vs. Good Prompt
❌ Bad Request
Make a payment feature
✅ Good Request
Implement a payment system using Stripe in a Node.js Express server.
Requirements
- Card payment (one‑time)
- Subscription payment (monthly/yearly)
- Payment‑status update via webhook
- Retry logic on failure (max 3 attempts)
- Payment‑history query API
- Refund processing
Technology Stack
- Node.js 18+
- Express 4
- TypeScript
- Stripe API v2023
- PostgreSQL
Error Handling
- Card declined
- Insufficient funds
- Network timeout
- Prevent duplicate payment
Security
- API keys in environment variables
- Webhook signature verification
- Rate limiting
- SQL‑injection prevention
Providing this level of detail yields code that can be dropped straight into a repo.
Typical AI‑Generated Artifacts
| Category | Examples |
|---|---|
| Boilerplate | CRUD scaffolding, config files, test templates |
| Algorithm Implementation | Sorting, searching, data transformation, math calculations |
| Documentation | Inline comments, README, API docs |
| Refactoring / Cleanup | Naming improvements, duplicate removal, architecture decisions |
| Advanced Topics | Scalability, performance tuning, technical‑debt management, creative problem solving |
| New Algorithms | Innovative approaches, special optimizations |
Think of the AI as a junior developer—you need to guide it.
Collaborative Workflow Example
Project: E‑commerce platform
Tech Stack: Next.js + NestJS + PostgreSQL
-
Define the first module
First, we'll make the product‑management API. 1. Product CRUD 2. Category management 3. Inventory tracking 4. Image upload -
Drill down to a concrete task
Let's start with the product‑creation API. POST /api/products Request body: { name, price, category_id, stock } Validation: price > 0, stock >= 0 Response: 201 Created with product object -
Iterate
- “Add transaction handling here.”
- “Improve error handling.”
- “Write unit tests.”
Work Breakdown Structure (WBS) for AI
When you give a large, vague request, AI returns “???”.
Break the work into small, well‑scoped tasks that AI can understand.
Make an e‑commerce site
→ AI: ??? (useless code)
Sample WBS
1. User Authentication
1.1 Signup API
1.1.1 Email duplicate check
1.1.2 Password encryption
1.1.3 Send verification email
1.2 Login API
1.2.1 Credential verification
1.2.2 JWT token issuance
1.2.3 Refresh token management
Each lowest‑level task (e.g., 1.1.1) is a size that AI can accurately implement.
My Prompt Template (What I Actually Use)
[Project Overview]
- Brief description
- Business goals
[Technology Stack]
- Language / framework versions
- External services (e.g., Stripe, AWS)
[Feature Description]
- High‑level purpose
- Detailed functional requirements (list)
- Non‑functional requirements (performance, security, etc.)
[Edge Cases & Validation]
- Input constraints
- Failure scenarios
[References]
- Links to docs, similar code, OpenAPI specs
[Deliverables]
- Code files (list)
- Tests (unit/integration)
- Documentation (README, API docs)
Copy, fill in the blanks, and feed the prompt to the AI. The result will be production‑ready code you can merge with confidence.
Option 1 – Bullet List (with bold labels)
## Context
- **Project:** [Project Name]
- **Current Task:** [WBS number and description]
- **Technology Stack:** [Language, Framework, DB]
Option 2 – Table (compact and easy to scan)
## Context
| Item | Details |
|---------------------|-----------------------------|
| **Project** | [Project Name] |
| **Current Task** | [WBS number and description] |
| **Technology Stack**| [Language, Framework, DB] |
Both versions preserve the original information while improving readability and visual consistency. Use whichever fits best with your documentation.
Requirements
Functional Requirements
- Requirement 1
- Requirement 2
Non‑Functional Requirements
- Performance: Response time, throughput
- Security: Authentication, authorization, encryption
- Scalability: Concurrent users, data growth
Constraints
- Compatibility with existing code
- External API limitations
- Library versions
Expected Input/Output
Input
[Example data]
Output
[Expected result]
Reference Code
// Existing code or similar example
Using this template increases AI accuracy by 3×.
Characteristics by AI Tool
| AI Tool | Advantages | Disadvantages | Typical Use |
|---|---|---|---|
| ChatGPT | • Excellent at explanations • Supports many programming languages | • Lacks up‑to‑date information | • Concept explanation • Algorithm design |
| GitHub Copilot | • Seamless IDE integration • Understands surrounding code context | • Generates only short snippets | • Autocomplete • Boilerplate creation |
| Claude | • Handles long code blocks • High accuracy | • Tends to be conservative in suggestions | • Complex logic implementation • Refactoring |
| Cursor | • Grasps the entire codebase • Good for large‑scale changes | • Requires a paid subscription | • Large‑scale refactoring • Project‑wide code improvements |
Conclusion: AI Is Just a Tool
Will AI replace developers? Absolutely not.
AI is a tool that amplifies power—just as a hammer can’t replace a carpenter, AI can’t replace developers.
Developers who use AI effectively can be 10× more productive than those who don’t.
The Key: Clear Requirements
- Break down work with a Work Breakdown Structure (WBS).
- Clearly define each task.
When requirements are explicit, AI becomes an amazing productivity aid.
“Garbage In, Garbage Out.”
Clear input → Clear output. This is the development methodology for the AI era.
Need efficient project management with AI? Check out Plexo.