跪拜 Guibai
← All articles
AI Programming · Agent · Artificial Intelligence

How an AI Agent Actually Reads a File (It's Not the Model)

By 不一样的少年_ ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

File access is the first real-world integration most Agents need, and getting it wrong—by trusting model output or skipping path validation—opens a direct security hole. Understanding the harness as the gatekeeper, not the model, is the foundation for safely adding write operations, shell commands, and network access later.

Summary

Large language models run on remote servers with no access to a local file system. A file-read operation in an AI Agent is a structured handoff: the model returns a Tool Call specifying which file it needs, and a local harness validates the path, reads the bytes, and injects the content back into the model's context window. The model never touches the disk.

This process involves five participants—user, model, harness, tool function, and file system—and splits every tool into two halves: a Schema that describes the function to the model, and an Executor that performs the real I/O. The harness enforces strict checks on tool type, tool name, and parameter validity before any code runs, and it normalizes paths to prevent directory traversal attacks.

A single read requires two model requests: one to get the Tool Call, and a second to supply the file content as a Tool Result matched by `tool_call_id`. This forms a minimal decision-action-feedback loop, but it is not yet a full Agent Loop because the implementation stops after one tool call instead of looping until the model stops asking for more tools.

Takeaways
A model's context contains only messages and tool descriptions; local files never enter automatically.
Every tool requires both a Schema (for the model to understand it) and an Executor (for the harness to run it).
A Tool Call is a structured JSON request from the model, not an executed action—the harness retains execution authority.
Path validation must resolve to absolute paths and check that the target stays within the workspace directory, using a separator-aware prefix check to avoid sibling-directory spoofing.
Single file reads are capped at 8,000 bytes to prevent token blowout and context pollution, not because models can't handle longer text.
Tool Results must carry the original `tool_call_id` so the model can match results to requests when multiple tools are in flight.
A single tool call plus a follow-up answer is a closed loop, but a true Agent Loop requires repeatedly checking for new Tool Calls and executing them until the model produces a final text response.
Conclusions

Many developers assume the model 'reads' a file, but the model only ever sees text that the harness explicitly places into the messages array—this mental model shift is the difference between building a safe Agent and a dangerous one.

The `additionalProperties: false` constraint in the Tool Schema is an underappreciated signal: it tells the model not to invent parameters, which reduces hallucinated tool calls before they reach validation.

Limiting file reads to 8,000 bytes is a pragmatic token-management strategy, but it also creates a silent failure mode where the model answers from truncated data without knowing what was cut—production systems need explicit truncation markers and ideally ranged reads.

The article's deliberate choice to handle only `tool_calls?.[0]` and stop is a useful teaching constraint, but it also highlights how many 'Agent' demos are really single-step tool invocations dressed up with a chat interface.

Concepts & terms
Tool Schema
A JSON object describing a tool's name, purpose, and parameters that is sent to the model as part of a request. It acts as a function contract the model can read to decide when and how to request a tool call.
Tool Call
A structured response from the model containing a tool name, a JSON-encoded arguments string, and a unique call ID. It represents a request to execute a tool, not the execution itself.
Tool Result
The output of a tool execution fed back into the model's message history using a `role: 'tool'` message with a matching `tool_call_id`. It converts real-world data into model-readable context.
Harness
The application-layer orchestrator that sends requests to the model, provides tool definitions, validates and executes tool calls, and returns results. It is the security and execution boundary between the model and the local system.
Agent Loop
A continuous cycle where the model receives context, optionally requests a tool, the harness executes it and returns the result, and the model decides whether to request another tool or produce a final answer. A single tool call does not constitute a loop.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗