Claude Code in Practice / How Claude Code works

The agentic loop and Claude's tools

Lesson 4 of 34. Open to read. No account needed.

A chat assistant answers from what you typed. Claude Code can do more: it reads files, runs commands and edits code, then looks at what happened and decides what to do next. That cycle is the agentic loop, and almost everything later in the course, from permissions to hooks to cost, is a way of shaping it.

By the end you can:

  • explain what is in Claude's context, and why its size matters;
  • explain how one turn proceeds through tool calls and tool results;
  • predict which tools a request will use, and check the prediction in the session's transcript.

Before you start

  • Sign in, and what leaves your computer is complete: you are signed in, the practice repository is cloned and trusted, and its tests show the one expected failure.
  • Work in a terminal in the practice repository's root folder, with the course's Python environment active (source .venv/bin/activate).

Tokens and the context window

Language models do not read characters or whole words. They read tokens: short pieces of text, often a word or part of one. Prices, limits and the amount a model can take in are all counted in tokens.

The context window is the most a model can consider at once, measured in tokens. Everything Claude knows during a request has to fit in it:

  • Claude Code's own system prompt, and the definitions of the tools Claude may use;
  • instruction files for the project, which you write in a later module;
  • the conversation so far, including your new request;
  • tool results: every file Claude read and every command output it saw.

Two consequences follow, and both shape how you work:

  1. Claude only knows what is in its context. It has not seen a file until it reads it, and it does not remember earlier sessions unless you resume them. If a fact matters, Claude has to read it or you have to give it.
  2. Context is sent on every step, and it grows. Each time Claude decides its next step, the whole context goes to the model again. Reading a 5,000-line log adds thousands of tokens that stay in the context for the rest of the conversation. Claude Code reduces the cost of resending unchanged parts with prompt caching, but a full context is slower, costs more and gives Claude more to sift through.

Here is what that looked like in one of the author's runs of this lesson's exercise (illustrative, Claude Code 2.1.308, sonnet alias). The question was about 40 words long. Across the run's three model requests, Claude Code reported:

Token countValueWhat it is
Input tokens read from the prompt cache55,345Unchanged context resent on each step: mainly the system prompt and tool definitions
Input tokens written to the cache13,240Context added during the run, such as the file contents Claude read
New, uncached input tokens6The small remainder
Output tokens1,047Claude's tool calls and its answer

Your question was a tiny part of what Claude processed. The next lesson, Context, compaction and usage, shows how to see this in your own sessions with /context and /usage.

How one turn works

The agentic loop: your request joins the context; Claude decides either to answer or to call a tool; Claude Code checks permissions, runs the tool and adds the result to the context; the loop repeats

One turn of the agentic loop.

Follow the arrows from the left:

  1. Your request joins the context with everything already there.
  2. The context is sent to Claude, the model, which decides the next step. It either answers, which ends the turn, or asks to use a tool.
  3. A tool call names a tool and its input, for example the Bash tool with the command cat src/tidewell/pricing.py.
  4. Claude Code, the program on your computer, checks the call against your permission settings. If the call needs your approval, it asks you. Then it runs the tool on your computer.
  5. The result is added to the context: the file's contents, the command's output, or an error. A refused call also comes back as a result, so Claude can try another way, as you saw in Requests Claude can verify when an unapproved test command was refused.
  6. The loop repeats until Claude answers, or a limit such as a turn or usage limit stops it.

You can press Esc at any step to interrupt Claude and redirect it. The work done so far is kept.

The important boundary in this picture is between Claude and Claude Code. Claude, the model, only proposes tool calls. Claude Code, on your computer, decides whether they run. That boundary is where permissions, the sandbox and hooks act, in later modules.

Claude's main tools

ToolWhat it doesAsks permission by default?
ReadReads a file's contentsNo
EditMakes a targeted change to a fileYes
WriteCreates or overwrites a fileYes
BashRuns a shell commandYes, except a built-in set of read-only commands such as ls, cat, find and grep
Glob and GrepFind files by name, and lines inside filesNo; on Windows only by default (see below)
WebFetch and WebSearchFetch a web page; search the webYes
AgentHands a task to a subagent with its own contextSubagents are covered later

On macOS, Linux and WSL, Claude Code leaves the separate Glob and Grep tools out by default. Claude searches with find and grep through the Bash tool instead, and those read-only commands run without asking. On native Windows, Glob and Grep are part of the default tool set. So the same question can produce different tool calls on different platforms.

"Asks permission by default" describes Manual mode, the mode you start with claude --permission-mode default. Other modes and your own rules change it; that is the subject of the permissions module.

Trace a turn

Goal: ask a read-only question about the practice repository, then check which tools Claude used and why, against your prediction.

1. Check the starting state

In the practice repository's root folder:

git status --short

It should print nothing.

2. Predict before you ask

The question you will ask is:

Explain how `PYTHONPATH=src python3 -m tidewell quote cargo 2` works out the price it prints: name each file and function involved, in the order they run. Read the code to answer; do not change any files.

Before you send it, write down:

  • which files Claude will need to read;
  • which tools you expect it to use on your platform;
  • whether any step should need your permission.

3. Ask the question

claude --permission-mode default

Paste the question and send it. Watch the tool calls appear as Claude works. When it has answered, press Ctrl+O to open the transcript viewer, which shows each tool call and its result in full, with timestamps. Press Esc or q to close it. Then leave the session with /exit.

If Claude asks permission to run the command itself, for example python3 -m tidewell quote cargo 2, decline: the question asks it to read the code, and the command is not in the read-only set.

4. Compare and check

In the author's two runs, on Linux, the trace was the same both times (illustrative, condensed):

Bash  find . -path ./.git -prune -o -type f -print && cat src/tidewell/__main__.py
                                     # list the project's files, read the entry point
Bash  cat -n bikes.py pricing.py __init__.py      (in src/tidewell)
                                     # read the two modules __main__.py imports

Then Claude answered: __main__.py parses the arguments, get_bike_type in bikes.py looks up the cargo bike, and hire_charge_pence in pricing.py computes the price. Both runs also spotted that the formula adds the free hour. Neither needed permission: find and cat are read-only commands.

Now check one detail yourself. One run's answer gave the location of the pricing function as pricing.py:33. Run:

grep -n "def hire_charge_pence" src/tidewell/pricing.py

Illustrative output:

8:def hire_charge_pence(bike_code: str, hours: int) -> int:

The function is on line 8. Line 33 was its position in the combined output of cat -n bikes.py pricing.py, the command Claude used to read both files at once. The answer was right in substance and wrong in a detail, and the reason is visible in its own tool result. This is why you check details against the files, not against Claude's summary.

Finally, confirm that nothing changed:

git status --short

It should still print nothing.

What success shows

  • Your predicted files and tools match the transcript, or you can explain each difference;
  • you can say what each tool call contributed;
  • git status --short prints nothing: the question changed no files.

This shows you can read a turn as a sequence of tool calls and results, which is how you will diagnose almost every later problem.

When your result differs

What you seeWhat it meansSafe next step
Claude uses Read, Glob or Grep tools instead of Bash commandsNormal: the tool set depends on platform and version, and Claude chooses its own routeCheck that each call still makes sense for the question
Claude asks to run the quote commandIt wants to observe the output rather than read the codeDecline; the question asks it to read the code
Claude asks to edit a fileIt treated the bug as a request to fix itDecline, and repeat "do not change any files"
The answer names a file Claude never readIt guessed from names or importsAsk it to read the file and confirm, or read it yourself
git status --short shows a changeA change was approved by mistakegit restore <file>, then git status --short again

Your turn

Before asking, predict the tools for this question, then ask it and compare:

Which files in this repository mention the late fee, and what do they say about it? Do not change any files.

Hint

Where is the late-fee rule written: in the code, the tests or the documentation? Which read-only commands find a word across many files?

Solution

Expect a search across the repository, such as grep -rn -i "late" through Bash on macOS, Linux and WSL, or the Grep tool on Windows, followed by reading the matching files. The rule lives in docs/staff-guide.md, and a TODO(feature) comment in src/tidewell/pricing.py notes the missing feature. No step should need permission. Claude's route may differ: judge it by whether each call helps answer the question, and confirm with git status --short that nothing changed.

Reset and cleanup

Nothing to clean up: the questions changed no files. If git status --short shows a change you approved by mistake, git restore <file> undoes it.

Check your understanding

  1. Claude Code answered a 40-word question after processing more than 68,000 input tokens. Where did they come from?
  2. Who decides whether a tool call runs: Claude or Claude Code?
  3. On Linux, Claude listed files with find and read them with cat. Why did it not ask permission?
  4. Why does a refused tool call not end the turn?

Answers

  1. Mostly from context Claude Code sends with every request: the system prompt, tool definitions and, during the run, the file contents Claude read. Most of it was served from the prompt cache.
  2. Claude Code. The model proposes a call; Claude Code checks it against your permissions, asks you if needed, and runs it.
  3. They are in Claude Code's built-in set of read-only commands, which run without a prompt in every mode.
  4. The refusal is returned to Claude as a tool result, so it can choose another way or explain why it cannot continue.

29 more lessons in the full course

Course access opens every lesson and adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 199 plus 18% GST. 90 days of online access. No auto-renewal.

Get the full course

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy