Giving an LLM Its First Tool: How to Let an AI Agent Read Your Project Files
Tool calling is the mechanism that turns a chatbot into an agent. This walkthrough strips away frameworks to show the exact handshake: a JSON Schema tool definition, a single OpenAI API call with `tools`, and a program-side switch that executes the requested function. Understanding that loop is prerequisite to building agents that read, write, and run commands autonomously.
A terminal-based AI agent built from scratch in TypeScript moves beyond blind Q&A. The agent now carries a `read_file` tool, described via JSON Schema, that lets it request file contents when it needs context it doesn't have. A Node.js backend catches the tool-call request, reads the real file from disk, and feeds the content back to the model for a grounded answer. The implementation deliberately limits reads to the project directory and caps file size at 8,000 bytes to control token costs and prevent directory traversal. The core loop — AI requests a tool, the program executes it, the result goes back to the model — is the foundation for multi-step agent behavior that will be expanded in later installments.
Frameworks like LangChain abstract this exact loop, but the underlying mechanism is only two API calls and a function dispatch — simple enough to build and debug directly.
Restricting file access to the project root and capping read size are not afterthoughts; they are essential safety and cost controls that must be designed in from the start.
The single-turn limitation is deliberate pedagogy: mastering one request-execute-return cycle makes the jump to a multi-turn agent loop a small, understandable step rather than a leap.