Sub topic 4
Sharing files
This chapter assumes you have basic programming experience, you work in an editor or terminal, and you have used an AI chat or coding assistant before. It covers when to share a whole file, which files to pick, and the mistakes that make answers worse.
Why share a file at all
An assistant can only reason about code it can see. If you paste one line and ask "why does this fail?", it has to guess what the rest of the program does. A file gives it the surrounding facts: imports, helper functions, naming, and style.
The file we use throughout this tutorial is small:
def average(values):
return sum(values) / len(values)
print(average([]))With only the line return sum(values) / len(values), the assistant can't know who calls average or what the callers expect. With the whole file, it can see that average([]) is called directly, and that nothing guards against an empty list.
When a whole file is the right choice
Share the whole file when the task depends on how the pieces fit together.
- The bug or change touches several functions. The assistant needs to see how they call each other.
- You want new code to match existing style. Naming, type hints, and error handling are visible only in real code.
- The file is short. A 40-line script costs little and removes guesswork.
- You are asking for a review or a refactor. Both need the full picture.
Share less when the file is large and your question concerns one function. The next chapter covers selections.
Which files to pick
Start with the file where the problem shows up. Then add other files only if the assistant would need them to answer correctly. A useful test is to ask whether a new teammate could do the task without reading that file. If they could not, include it.
| File | Include? | Reason |
|---|---|---|
| The file that raises the error | Yes | It holds the failing code. |
| A module the failing code imports and calls | Often | The behavior of the call matters. |
| A test file for the code | Yes, for tests or behavior changes | It shows expected behavior. |
| Unrelated modules in the same folder | No | They add noise and use up space. |
| Generated files, lock files, build output | No | They are long and rarely relevant. |
Making a file easy to use
Tell the assistant which file you are sharing and what part of it matters. A file attached with no comment leaves the assistant to decide where to look. Name the file, and point to lines when you can.
Context: stats.py (attached). Focus on the average function, lines 1-2.
Task: Explain why average([]) fails and suggest a fix.If you paste the file into a chat box instead of attaching it, put the file name on a line before the code. Without the name, the assistant cannot tell which file a later answer refers to.
Common errors
- Sharing the wrong version. If you edited the file after the last attach, the assistant may be reading old code. Re-attach it after changes.
- Sharing too many files. Each extra file competes for attention and for the context budget. The answer often gets vaguer, not better.
- Sharing too few files. The assistant then invents what the missing code does. If an answer mentions a function you did not show, treat that part as a guess.
- Leaving out the task. A file alone does not say what you want. Always add a sentence about the goal.
- Sharing files with secrets. Files such as
.envor configuration with keys should not be sent. Chapter 11 covers this.
Check your understanding
Exercise
You are debugging a ZeroDivisionError in a 600-line module. The failing function is 8 lines long and calls one helper defined in the same file. Which would you share, and why?
Show answer
Share the failing function and the helper it calls (or the part of the file that contains both), plus the error. The 600-line file as a whole adds noise and uses budget. If the function depends on module-level settings or imports, add only those lines.
Exercise
An assistant's fix references a function called validate_input that you never showed it. What does this tell you, and what should you do?
Show answer
The assistant guessed at code it could not see. Check whether validate_input exists and what it does. If it matters, share the file that defines it and ask again.