Sub topic 4
Sharing files
This chapter assumes you have written and run small programs, can use an editor or terminal, and have used an AI chat or coding assistant at least a few times. It covers when and how to share a whole file with an assistant, what that costs, and where it goes wrong.
What file context is
File context means giving the assistant the contents of a file, either by pasting the text or by attaching or referencing the file in your tool. The assistant cannot see your disk unless you or your tool provide the file. Without it, the assistant answers from general knowledge and guesses about your code.
A file gives the assistant things a short description cannot:
- The exact names of functions, variables, and imports.
- The style of the surrounding code, such as naming and error handling.
- The structure around the problem, such as what a function's callers pass in.
- The literal text, so that its suggested edits match what is really there.
Here is the small file used throughout this tutorial. It has a bug: it fails on an empty list.
def average(values):
return sum(values) / len(values)
print(average([]))If you ask "my average function crashes, fix it" and share nothing else, the assistant must invent a function. If you attach stats.py, it can see the real code and propose a change to the real lines.
When to share a whole file
A whole file is the right choice when the task depends on how the parts fit together. Common cases:
- The change touches several functions in the file.
- The bug may come from an import, a global, or code that runs at module level.
- You want the assistant to follow the file's existing style when it adds code.
- You are asking for a review or an explanation of the file as a whole.
A whole file is the wrong choice when the problem lives in a few lines and the rest is unrelated. The next chapter covers selections for that case. A useful rule: share the smallest unit that still contains everything the answer depends on. For a small file like stats.py, that unit is the whole file. For a 3,000-line module, it usually is not.
The cost of a file
Every file you share uses part of the assistant's context window, the fixed amount of text it can consider at once. A long file leaves less room for the rest of your request and for the answer. A large file also dilutes the signal: the assistant has more text to sort through, and relevant details can get less attention.
Size is not the only cost. Each extra file adds more chances for irrelevant or conflicting information. Sharing ten files "just in case" often gives worse answers than sharing two well-chosen ones.
The following table gives rough guidance. Treat the line counts as starting points, not rules.
| Situation | Reasonable choice |
|---|---|
| File under a few hundred lines, task touches most of it | Share the whole file |
| Long file, task touches one function | Share a selection, plus a note on what the file does |
| Bug involves two files interacting | Share both, or the relevant parts of both |
| Question about a library or API | Share the call site and the error, not the library |
| Generated, minified, or vendored file | Do not share it |
Choosing which files
Pick files by following the problem, not by guessing what might help. Start with the file where the error or the behavior shows up. Then add a file only when you can say why it is needed.
Reasons that justify adding a second file:
- The first file imports a function from it, and the bug may be inside that function.
- It defines a type, schema, or interface that the first file must conform to.
- It contains the test that fails.
- It contains configuration that changes the behavior, such as a feature flag or settings file.
Reasons that do not justify it: "it is in the same folder", "it might be related", or "I always attach it".
A quick check before sending: for each file, finish the sentence "The assistant needs this file because ...". If you cannot finish it, leave the file out.
Telling the assistant what the file is for
A file alone does not say what you want. State what the file is and what you want done with it, in a sentence or two. Compare:
Here is a file. Fix it.Context: stats.py (attached), a small module of statistics helpers.
Task: Make average handle an empty list without crashing.The second version names the file, says what it is, and states the task. The assistant no longer has to guess which part of the file you care about or what "fix" means.
If the file is not self-explanatory, add one line about its role: "this is the entry point", "this is called from the web handler", or "this runs on Python 3.8". Short facts like these are often worth more than extra files.
Sharing by paste and by reference
How you share depends on your tool. There are two common ways.
Paste. You copy the text into the message. This works everywhere and gives you full control. The drawbacks are that you lose the file name, and a pasted copy can go out of date as soon as you edit the file. When you paste, put the file name on a line before the code so the assistant knows what it is looking at:
File: stats.py
def average(values):
return sum(values) / len(values)
print(average([]))Attach or reference. Many editors and command-line assistants let you attach a file by path or pick it from a list. The tool reads the current contents from disk. This keeps the file name and avoids copy errors. It also means the tool may send more than you expect, so check what is attached before you send.
Some assistants can read files on their own when they run in your project. That is convenient, but it is not a reason to stop choosing. Telling the assistant which file to look at still saves time and reduces wrong guesses.
Edge cases
Files that are too large
If a file exceeds the context window, the tool may cut it off without a warning, or reject it. Truncation is dangerous because the assistant answers as if it saw the whole file. If you suspect a file is long, share the relevant sections and say so: "This is lines 200 to 340 of a 1,200-line file."
Stale copies
If you paste a file, edit it, and keep chatting, the assistant may still work from the old version. Its suggestions can then refer to lines that no longer exist. After a significant edit, paste or re-attach the file and say that it replaces the earlier version. Chapter 10 covers this in more detail.
Several files with the same name
Projects often contain more than one __init__.py, index.js, or utils.py. Include the path in the label, such as app/utils.py, so the assistant does not mix them up.
Files that import other files
The assistant sees only what you give it. If stats.py imports helpers.py and the bug may be in the helper, the assistant cannot check the helper unless you share it. When the assistant makes an assumption about code it has not seen, it should say so. If it does not, ask: "Which parts of your answer assume code you have not seen?"
Files with secrets
Configuration files, environment files, and scripts often hold passwords, API keys, or tokens. Sharing the file sends those values to the assistant's service. Remove or replace them before sending. Chapter 11 treats this fully. For now, make it a habit to scan any config-like file before you attach it.
Binary and generated files
Compiled output, images, lock files, and minified bundles waste space and rarely help. Share the source that produced them instead.
A worked example
Suppose stats.py is part of a project with a test file, test_stats.py:
from stats import average
def test_average_of_numbers():
assert average([2, 4, 6]) == 4
def test_average_of_empty_list():
assert average([]) is NoneThe second test fails because average([]) raises an error. Which files does the assistant need?
stats.py, because it contains the function to change.test_stats.py, because it states the expected behavior: an empty list returnsNone.
It does not need the rest of the project, the README, or the other modules. A good request looks like this:
Files: stats.py and test_stats.py (both attached).
Task: test_average_of_empty_list fails. Change average in stats.py so an empty list returns None. Keep the function signature and do not edit the tests.A reasonable answer from the assistant is:
def average(values):
if not values:
return None
return sum(values) / len(values)
print(average([]))Check it yourself. Run the tests with pytest, and confirm both pass. If you share the test, you can verify the answer in seconds, and that is a good reason to include test files when they exist.
Common mistakes
- Sharing everything. The whole project is attached, and the answer is vague. Fix: pick files by the reasons listed earlier.
- Sharing nothing. The assistant invents code that does not match yours. Fix: attach at least the file where the problem appears.
- Sharing an old copy. The answer refers to code you already changed. Fix: re-attach after edits.
- Unlabeled paste. The assistant cannot tell which file is which. Fix: add the path before each file.
- No task. The assistant summarizes the file when you wanted a change. Fix: state the task and its limits.
Summary of the working model
Treat each file as something that must earn its place. Share the smallest set of files that contains what the answer depends on, label them with paths, say what you want done, and re-share them after significant edits. Include the test that defines correct behavior when there is one, because it lets you check the answer.
Exercise
You have a 900-line orders.py and a failing test in test_orders.py. The failure is in a function apply_discount of about 15 lines. apply_discount calls round_money, which is defined in money.py. The project also has README.md, config.yaml (which holds a payment API key), and shipping.py. Which files, or parts of files, do you share, and which do you leave out? Give one reason for each decision.
Show answer
Share the apply_discount function from orders.py (a selection, not all 900 lines), with a note of where it sits in the file. Share test_orders.py (or the failing test), because it defines the expected behavior. Share round_money from money.py, because apply_discount calls it and the bug may be inside it. Leave out README.md and shipping.py, because nothing links them to the failure. Leave out config.yaml, because it is unrelated and contains a secret that should not be sent. If the whole of orders.py turns out to matter, for example because of module-level state, share more of it later and say why.
Exercise
You paste stats.py into a chat, ask for a fix, and get one. You then edit the file by hand, ask a follow-up, and the answer mentions a line that no longer exists. What happened, and what do you do?
Show answer
The assistant is still working from the earlier pasted copy, which is now stale. Paste or re-attach the current version of stats.py with its file name, and state that it replaces the previous version. Then repeat the follow-up question.