How do you ask an AI to explain code you did not write?
To get an AI to explain code you did not write, give it the file and the code around it, say what you need to understand and at what level, and ask for a walk-through tied to line numbers with an example input and output. Then run the code to check the claims: an explanation can be fluent and wrong.
Cite
CodeLifter Forum, "How do you ask an AI to explain code you did not write?", https://codelifter.com/forum/posts/how-do-you-ask-an-ai-to-explain-code-you-did-not-write/, accessed 2026-11-19.- CodeLifter editorsStaffStaff, CodeLifter ·
What should you give the assistant?
An assistant explains what it can see. If it has read the function but not the function that calls it, it fills the gap with a guess, which is the same mechanism behind invented functions. How much it can see at once is its context window, covered in the context window thread.
So give it, in roughly this order:
- The code itself, as a file reference rather than a paste where the tool supports it. In Claude Code,
@src/utils/auth.jsincludes the file; the common workflows page uses "Explain the logic in @src/utils/auth.js" as its example. In Copilot Chat in Visual Studio,#MyFile.cs: 66-72references specific lines, per GitHub's cheat sheet. - The callers and the data it receives. "This is called from
routes/login.jswith the parsed request body" saves a round of guessing. - The version of the language and main libraries, from
package.jsonor your lockfile. - What you already know and what you are trying to do. "I know JavaScript but not this framework; I need to change how sessions expire" gets a different, better answer than "explain this".
Leave secrets out. If the code you are pasting contains keys or connection strings, remove them first; the thread on keeping API keys out of a vibe-coded app covers why.
Which prompt fits which question?
Most confusion about unfamiliar code is one of five questions. The example prompts in this table come from Anthropic's common workflows and best practices pages; they work in any assistant with the file in view.
Your question Prompt What to check in the answer What is this project? "give me an overview of this codebase" The entry points it names actually exist How does one feature flow? "trace the login process from front-end to database" Each step names a real file and function What does this line do? "What does async move { ... }do on line 134 offoo.rs?"Matches the language reference Why is it written this way? "look through ExecutionFactory's git history and summarize how its api came to be" The commits it cites exist What do these names mean? "Request a glossary of project-specific terms" Terms appear in the code, not just the answer The best practices page frames it plainly: ask the questions "you'd ask another engineer", and "No special prompting required".
How do you work through one unfamiliar file?
- Start read-only. If the assistant can edit files, stop it from doing so while you are learning. In Claude Code,
claude --permission-mode planstarts a session where it "reads files and answers questions without making changes", per the best practices page. In a chat window, simply do not ask for changes. - Ask for the overview first, in three or four sentences: what the file is for, what goes in, what comes out.
- Ask for a walk-through with line numbers. "Walk through
parseConfigblock by block, citing line numbers, and say what each block assumes about its input." Line numbers make every claim checkable. - Ask for one worked example. "Show the value of
resultafter each step for the input{ retries: '3' }." A wrong explanation usually falls apart on a concrete input. - Run it. Add a
console.logor a breakpoint at the step the assistant described and see whether the value matches. This is the step that turns an explanation into knowledge. - Read the history.
git log -L :parseConfig:src/config.jstraces the evolution of one function; the git log documentation (git 2.56.0) describes-L:<funcname>:<file>as tracing "the evolution of the line range given by... the function name regex". Commit messages often explain the odd parts better than any model can. - Explain it back. Write your own two-paragraph summary and ask the assistant what it gets wrong. That turns the session from reading into checking.
How do you know the explanation is right?
You do not, until something independent agrees with it: the code running, the official docs, a test, or the commit history. Some signs an explanation needs checking:
- It describes what a well-named function should do rather than what this one does.
- It explains a library call you cannot find in that library's reference.
- It is confident about code it was never shown, such as "this calls the database layer", when you did not include that file.
- It skips a block. Ask about the skipped lines directly.
Asking "which parts of this explanation are you least sure about?" is a cheap extra prompt. The answer is a list of places to look first, not a guarantee about the rest.
Does asking for explanations help you learn?
It can, if you do steps 4 to 7 yourself. Reading an explanation feels like understanding; running the example and explaining it back is what makes it stick. The thread on whether vibe coding is bad for learning to program covers that trade-off in more depth. For old cut-and-paste JavaScript, CodeLifter's script shelf keeps the originals with notes on what each one does, which is a good test set: ask for an explanation, then compare.
Short version
- Give the code plus its callers, versions and your goal; leave secrets out.
- Ask one of five questions: overview, flow, line, history or glossary.
- Insist on line numbers and a worked example, then run the code to check.
- Use
git log -Lfor the why; explain it back to find the gaps.
- The code itself, as a file reference rather than a paste where the tool supports it. In Claude Code,
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
1 answer
- CodeLifter editorsStaffStaff, CodeLifter ·
How do you use /explain in GitHub Copilot? Select the code in your editor, open Copilot Chat, type
/explainand press Enter. GitHub's Copilot Chat cheat sheet describes the command in VS Code, Visual Studio and JetBrains as "Explain how the code in your active editor works", and in Xcode as "Provide an explanation for the selected code." It also notes that "Available slash commands may vary, depending on your environment", and that typing/in the chat box lists the ones you have.You can add your own words after the command, which is where the opening post's advice goes:
/explain what happens when token is undefined, citing line numbers. To point at code that is not selected, the Visual Studio cheat sheet shows references such asExplain this function #MyFile.cs: 66-72?andHow are these files related #MyFile.cs #MyFile2.cs. If the answer refers to a file you did not reference, ask whether it read that file or is assuming; then open it yourself. The same checks apply as for any explanation: a worked example, a run, and the docs for any library call it names.