Sub topic 3
Add the context the assistant cannot see
This chapter assumes you can write and run code in at least one language, run tests, and have used a chat-based or editor-integrated AI assistant. It builds on the previous chapter, where you wrote a specific, testable goal.
Why context matters
The assistant sees only what you send it. It cannot open your project, check which Python version you run, or read the error in your terminal. When information is missing, it fills the gap with a guess. Guesses tend to be the most common choice, and the most common choice is often wrong for your project.
A goal such as "Write a function to clean up titles" leaves open the language, the version, the input type, and what "clean up" means. Context closes those gaps before the assistant has to guess.
What counts as relevant context
Include facts that change the answer. Leave out facts that do not.
| Kind of context | Example | What it prevents |
|---|---|---|
| Language and version | Python 3.10 | Syntax or library calls that your version lacks |
| Framework or runtime | Flask 3, Node 20 | Code written for a different framework |
| Existing code | The function you want changed | Rewrites that break your callers |
| Error message | The full traceback | Guessing at the cause |
| Environment | Linux container, no network | Solutions that need a missing resource |
| Purpose | A small blog tool, ASCII titles | Over-engineering or under-handling input |
Worked example: adding context
Start from a weak prompt and add context one piece at a time.
Write a function to clean up titles.Now add the language, signature, and purpose:
Write a Python 3.10 function slugify(text: str) -> str that converts an article title into a URL slug.
Context: it will be used in a small blog tool; titles are plain ASCII.The assistant now knows the language, the exact signature, and the kind of input. It will not add Unicode handling you did not need, and it will not return a different type. The constraints that complete this prompt come in the next chapter.
Paste the real material
When you ask for a fix, paste the code and the error. Do not describe them from memory. A description loses detail, and the lost detail is often the cause.
Task: fix the failing test.
Context: Python 3.10, pytest. Relevant code and error below.
def slugify(text):
return text.lower().replace(" ", "-")
Error:
AssertionError: assert 'hello,-world!' == 'hello-world'With the code and the failure in view, the assistant can see that punctuation is not handled. Without them it might suggest a change to the test.
Trim what you paste. Include the function under change, the code it calls, and the exact error. A whole repository adds noise and can push important lines out of the assistant's reach.
How context flows into the answer
Common mistakes
- Assuming the assistant remembers your project. In a new conversation, repeat the facts that matter. Even within one, state again anything that changes.
- Leaving out the version. Libraries change between versions. An answer for one version may fail on another.
- Summarizing instead of pasting. "It throws a type error somewhere" gives the assistant nothing to work with.
- Including too much. Unrelated files dilute the useful parts. Pick what bears on the task.
- Including secrets. Context must not contain passwords, keys, or private data. A later chapter covers how to remove them.
A quick checklist
Before sending a prompt, ask yourself:
- Did I name the language and version?
- Did I include the code the change touches?
- Did I include the exact error, if there is one?
- Did I say where the code will run and who will use it?
- Did I remove anything private?
Exercise
You ask an assistant, "Why does my script crash?" and describe the crash in one sentence. The answers are vague. Name three pieces of context to add, and say why each helps.
Show answer
Any three of the following. The full error message or traceback, because it names the error type and the line where it happened. The relevant code, because the assistant can only reason about code it can see. The language and version, because behavior and available libraries differ between versions. The environment, such as the operating system or whether a network is available, because some failures depend on it.
Exercise
Rewrite this prompt so it supplies context: "Add caching to my function." List the context you would add, then write the improved prompt.
Show answer
Add the language and version, the function's code, what it computes and how often it is called, and where it runs. Example: "Task: add caching to the function below. Context: Python 3.10, standard library only, called many times with repeated integer arguments in a single process. Code: <paste function>." The assistant can then choose a suitable tool, such as functools.lru_cache, instead of guessing.