How AI Thinks About Code

The mental model every AI coding beginner needs. Understand this and you will immediately get better results from every prompt you write.

Beginner 9 min read Lesson 4 of 20

AI is Not Magic

The single biggest mistake beginners make is treating AI like a mind-reader. They write vague prompts and expect perfect results. When results are wrong, they conclude "AI is bad." The real issue is that the AI only knows what you tell it.

An LLM (Large Language Model) like Claude is a statistical prediction engine. It predicts the most probable sequence of words (tokens) that should follow your input, based on patterns learned from a massive amount of text including code. It is extraordinarily good at this - but it is prediction, not understanding.

The spectrum from bad to great prompts
# Vague - AI guesses what you mean (often wrong):
"Fix my code"
"Add authentication"
"Make it better"

# Somewhat clear - AI produces something generic:
"Write a login form in PHP"
"Add user authentication to my app"

# Precise - AI produces exactly what you need:
"In UserController.php, the login() method on line 34 fails with
 'Undefined variable $remember' when the checkbox is unchecked.
 Fix it to default $remember to false when the POST value is missing."

Rule: the more specific you are, the better the result.

The Context Window

The context window is the AI's "working memory" - everything it can see at once during your session. Think of it as a whiteboard that can hold a finite amount of text.

Everything in the context window:

  • Your CLAUDE.md (project background)
  • All messages in the current conversation
  • Any files the AI reads during the session
  • All tool call results (grep output, test results, etc.)
Context window analogy
Imagine hiring a contractor who can only work from the documents
you put on a table in front of them.

If you put 1 relevant document:
  - They understand the task precisely
  - Their work is focused and accurate

If you put 50 documents (most irrelevant):
  - They must guess which ones matter
  - They may use the wrong ones
  - Their work is diluted and inconsistent

AI context works the same way.
Give the AI exactly what it needs - nothing more, nothing less.

What the AI Actually Sees

When you type a prompt in Claude Code, here is what is actually sent to the AI model:

What Claude receives (simplified)
[SYSTEM PROMPT]
You are Claude Code, an AI coding assistant...
Project context from CLAUDE.md:
  - PHP 8.2 web application
  - MySQL database via PDO
  - Bootstrap 5 frontend
  [... rest of CLAUDE.md ...]

[CONVERSATION HISTORY]
User: Create a login form
Assistant: I'll create a login form. [reads auth.php...] Here is the form:
  [the code it wrote]
User: The form submits but the session is not set

[CURRENT MESSAGE]
User: The form submits but the session is not set

[AVAILABLE TOOLS]
- Read file
- Write file
- Search (Grep)
- Run bash command
- Edit file

The AI sees text, not your actual running application. It does not see your browser, your database state, or anything not explicitly in the context. This is why "the session is not set" is a better bug report than "it does not work."

Why AI Makes Mistakes

Understanding error types helps you fix them faster:

Mistake typeCauseFix
Wrong assumptionAI guessed at unstated requirementsBe more specific in your prompt
Outdated approachTraining data had older patternsSpecify versions: "PHP 8.2, PDO not mysqli"
Hallucinated functionAI invented a function that does not existTest all AI code before trusting it
Context mismatchAI read the wrong file or no filesTell it exactly which file/line to look at
Cascading errorFirst output was wrong, AI built on itCorrect early; start a new session if stuck
Incomplete outputOutput was cut off at context limitAsk "continue from where you stopped"

Giving Good Context

Four things to include in any prompt for better results:

The four-part prompt formula
1. WHERE: Which file, function, or line number
   "In app/models/UserModel.php, the create() method..."

2. WHAT: What you want to happen
   "...should validate that email is unique before inserting."

3. CONSTRAINT: What must not change or must be true
   "Use the existing PDO connection ($this->db). Do not change the method signature."

4. OUTPUT: What you want back
   "Show only the changed lines as a diff."

Combined example:
"In app/models/UserModel.php, the create() method should validate
 that the email is unique before inserting. Use the existing PDO
 connection ($this->db). Do not change the method signature.
 Show only the changed lines as a diff."

Iteration is Normal

Professional AI users iterate constantly. The AI is rarely perfect on the first try for complex tasks - and that is expected. Each iteration adds context and brings the output closer to what you need.

Normal iteration pattern
Turn 1: "Create a user registration form in PHP with email and password"
-> AI creates a form. Good structure but no validation.

Turn 2: "Add validation: email must be valid format, password minimum 8 chars"
-> AI adds validation. Works but error messages are in English.

Turn 3: "Change error messages to Tamil. Email: 'சரியான மின்னஞ்சல் முகவரியை உள்ளிடவும்'"
-> AI updates messages. Done.

This 3-turn exchange took 3 minutes total.
Without AI it would have taken 45 minutes.
The best prompt is a conversation, not a monologue

Do not try to write one perfect mega-prompt that covers everything. Start simple, get something working, then refine with follow-up prompts. This mirrors how you would work with a human developer - you do not email them a 500-word spec; you have a conversation.

Frequently Asked Questions

Only within the same session. When you close Claude Code and open it again, the AI starts fresh with no memory of what you discussed before. This is why CLAUDE.md is important - it gives the AI the same background information at the start of every session without you having to re-explain everything.

Not automatically. The base AI model is trained on internet data up to a cutoff date and does not update from your sessions. However, you can guide its style by: (1) showing examples in your CLAUDE.md ("use this pattern: ..."), (2) starting sessions by saying "match the style of existing code", (3) using the memory system in Claude Code to save preferences.

The AI does not inherently know what you already have unless it reads your files. If you ask "add a login feature" without giving context, it generates a complete implementation from scratch. If it reads your existing auth.php first, it will extend what you have. Always tell the AI what already exists.

When you use Claude Code, your code is sent to Anthropic's servers for processing. Anthropic has privacy and data handling policies - review them at anthropic.com for current terms. For highly sensitive codebases (banking, healthcare), check your company's policy on sending code to third-party AI services.