Code of the Day
BeginnerYour First AI Conversation

Giving context

Context transforms AI responses from generic to precisely useful — learn what to include, what to leave out, and why it matters.

Using AIBeginner9 min read
Recommended first
By the end of this lesson you will be able to:
  • Explain why context changes the quality of AI responses on complex tasks
  • Identify the most useful types of context to include in a prompt
  • Distinguish relevant context from irrelevant preamble
  • Construct a context-rich prompt for a realistic coding scenario

"Write a function" gives the AI a task. "I'm building a Python command-line tool that processes CSV files exported from our accounting software; write a function that reads one of those files and returns the rows where the Amount column is negative, as a list of dicts" gives the AI a task and the information it needs to do it well.

Context is the difference between a generic answer and a precisely useful one. This lesson is about what context is, why it works, and how to include the right amount without padding.

Why context changes responses

The model you are talking to was trained on text from an enormous variety of domains, audiences, and use cases. When you send a bare question, the model has to estimate which version of an answer fits: beginner or expert? Python or JavaScript? Simple example or production-quality code? Thorough explanation or quick answer?

Every one of those choices is a guess. Add context, and the model no longer has to guess — it has the information it needs to produce the appropriate response on the first try.

This is not a trick. It is the same reason that telling a colleague "I need help with our payment processing code" gets a better response than saying "I need help with code." You are giving them the frame that makes their knowledge applicable to your situation.

What to include

Your background and skill level. "I'm learning Python and just covered functions" produces a different and more appropriate answer than sending the same question with no context. The model will calibrate vocabulary, example complexity, and assumed knowledge.

What you are building and why. The intended use case changes what "good" means. A function used once in a script has different requirements than one deployed in a production API. "This is a quick analysis script" signals something different from "This runs inside a Flask web application."

The constraints that matter. No external libraries. Must run in Python 3.8. The output has to be JSON-serialisable. Must handle Unicode filenames. Constraints narrow the solution space and prevent the model from giving you an answer that would work in the abstract but fails your actual situation.

The format you want. "Give me just the function, no explanation" is valid context. So is "Explain each line with a comment." So is "Show me two approaches and explain the trade-off." The model defaults to a format; if you want something different, say so.

What you have already tried. "I tried using str.split() but it fails when there are consecutive spaces" saves a round trip — the model will not suggest the thing you just told it doesn't work, and it now understands something specific about the problem.

What to leave out

Context improves responses only when it is relevant. There are common ways people add length without improving quality:

  • Extensive preamble: "I hope you can help me, I've been struggling with this for hours and I'm quite new to programming..." — the model cannot use any of this. State the task.
  • Information that doesn't change the answer: "I'm building this for a school project due Friday" rarely changes what a correct function looks like. Include constraints that would affect the solution; omit context that wouldn't.
  • Repeating yourself: Restating the same constraint three times does not emphasise it — it just adds tokens.

The target is the minimum context that rules out wrong assumptions. Everything beyond that is noise.

A useful test: for each piece of context you are about to add, ask "would removing this change the response in a way that matters?" If not, leave it out. If yes, it belongs.

A before-and-after comparison

Here is the same underlying task at two levels of context:

Without context:

Write a function to parse dates.

The model will produce something that handles common date formats, probably in whatever language is most common in its training data, with whatever format it considers standard. You may spend three rounds of follow-up correcting the language, the format string, and the edge cases.

With context:

I'm writing a Python script that processes log files from a web server. The timestamps in those logs look like 2024-03-15T14:22:05Z (ISO 8601, always UTC). Write a function that takes one of those timestamp strings and returns a Python datetime object in UTC. Use only the standard library — no third-party packages.

The second prompt is five times longer, but nearly all that length is relevant. The model knows the language, the input format, the return type, the source of the data, and the dependency constraint. It can produce a correct answer immediately.

Giving context about code you already have

One of the most powerful uses of context is pasting in your existing code. Instead of describing a problem abstractly, you show it directly:

Here is my current function:

def process_records(records):
    for record in records:
        if record['status'] == 'active':
            send_email(record['email'])

It raises a KeyError if any record is missing the status key. Add handling for that case — skip the record and log a warning to stderr.

Pasting the code removes the ambiguity completely. The model can see exactly what you have, what the issue is, and what change you need. This is nearly always faster than a prose description of the same situation.

Do not paste confidential code, credentials, or personal data into a public AI interface. If your codebase contains secrets, remove them from any snippet you paste. Check your company's policies on what can be shared with external AI services.

Check your understanding

Knowledge check

  1. 1.
    Why does adding relevant context to a prompt improve AI responses?
  2. 2.
    Which of the following are useful types of context to include in a coding prompt?
  3. 3.
    You should paste confidential credentials or personal data into an AI chat interface if it helps explain the problem.

Where to go next

You can now craft prompts with the right level of context. The next lesson covers the other side of the loop: reading and verifying AI output — how to evaluate what you get back before you act on it.

Finished reading? Mark it complete to track your progress.

On this page