Load Files and Add Instructions
In this project, you have 3 dummy call transcripts in the lib/calls directory that you want to load as context to the sandbox before asking the agent a question. You also need clear instructions so the agent knows to use the bash tool to explore the files and answer questions.
Outcome
You have a complete lib/agent.ts that loads call files into the sandbox and instructs the agent to use bash to explore them.
Fast Track
- Write a
loadSandboxFilesfunction that readslib/calls/and writes each file into the sandbox - Call
loadSandboxFilesbefore creating the agent - Add an
INSTRUCTIONSstring telling the agent to usebashToolto explore files and answer questions
Hands-on Exercise 2.2
Complete lib/agent.ts by adding file loading and instructions.
Requirements:
- Import
pathandfs/promisesfor filesystem operations - Write a
loadSandboxFilesfunction that reads every file fromlib/calls/and writes them into the sandbox atcalls/<filename> - Call
loadSandboxFiles(sandbox)before the agent is created - Add an
INSTRUCTIONSconstant that tells the agent its role and how to use the bash tool - Pass
INSTRUCTIONSto the agent'sinstructionsproperty
Implementation hints:
- Use
process.cwd()to build the path tolib/calls/ fs.readdirreturns filenames,fs.readFilereturns a buffersandbox.writeFilestakes an array of{ path, content }objects- The
loadSandboxFilesfunction can be defined after the agent export (hoisting), but it must be called before the export
Writing files to the sandbox
Create a function called loadSandboxFiles in agent.ts to load the call files into the sandbox:
import path from 'path';
import fs from 'fs/promises';
async function loadSandboxFiles(sandbox: Sandbox) {
const callsDir = path.join(process.cwd(), 'lib', 'calls');
const callFiles = await fs.readdir(callsDir);
for (const file of callFiles) {
const filePath = path.join(callsDir, file);
const buffer = await fs.readFile(filePath);
await sandbox.writeFiles([{ path: `calls/${file}`, content: buffer }]);
}
}What this function does is build the full path of the calls folder (in the same lib directory as agent.ts). Then it reads all filenames in that folder, loads each file as a buffer, and writes them to the sandbox's file system.
Be sure to call the function before the agent is created:
await loadSandboxFiles(sandbox);Agent instructions
Now that you have everything wired up, write clear instructions for the agent. You want it to use the bash tool to generate and execute commands to explore all the call transcripts to answer the user's questions:
const INSTRUCTIONS = `
You are a helpful assistant that answers questions about customer calls. Use bashTool to explore the files and find relevant information pertaining to the user's query. Using the information you find, craft a response for the user and output it as text.
`;Add the INSTRUCTIONS to the agent:
export const agent = new ToolLoopAgent({
instructions: INSTRUCTIONS,
// ...
});Try It
-
Restart the dev server and open
http://localhost:3000. -
Ask: "what files are available?"
The agent should call
bashToolwithls calls/and report the three transcript files:[bashTool] $ ls calls/ 1.md 2.md 3.md -
Ask: "summarize the first call"
The agent should
cat calls/1.md(or similar) and produce a summary of the call transcript: participants, topics discussed, outcomes. -
Ask: "did anyone mention pricing?"
The agent should
grepacross the files for pricing-related content and synthesize an answer.
If the agent lists files, reads transcripts, and answers questions, you've built a working filesystem agent. The next lesson covers testing strategies and extension ideas.
Commit
git add lib/agent.ts
git commit -m "feat(agent): load call files and add instructions"Done-When
loadSandboxFilesreads all files fromlib/calls/and writes them into the sandboxINSTRUCTIONStells the agent its role and how to usebashTool- The agent can list files in the sandbox (
ls calls/) - The agent can read and summarize call transcripts
- The agent can search across files to answer questions
Solution
Here's the complete lib/agent.ts file with all the pieces together:
import { ToolLoopAgent } from 'ai';
import { createBashTool } from './tools';
import { Sandbox } from '@vercel/sandbox';
import path from 'path';
import fs from 'fs/promises';
const INSTRUCTIONS = `
You are a helpful assistant that answers questions about customer calls. Use bashTool to explore the files and find relevant information pertaining to the user's query. Using the information you find, craft a response for the user and output it as text.
`;
const sandbox = await Sandbox.create();
const MODEL = 'anthropic/claude-opus-4.6';
await loadSandboxFiles(sandbox);
export const agent = new ToolLoopAgent({
model: MODEL,
instructions: INSTRUCTIONS,
tools: {
bashTool: createBashTool(sandbox)
}
});
async function loadSandboxFiles(sandbox: Sandbox) {
const callsDir = path.join(process.cwd(), 'lib', 'calls');
const callFiles = await fs.readdir(callsDir);
for (const file of callFiles) {
const filePath = path.join(callsDir, file);
const buffer = await fs.readFile(filePath);
await sandbox.writeFiles([{ path: `calls/${file}`, content: buffer }]);
}
}And the agent is complete! There is already a chat input and API route created to kick off the agent and stream back the results.
Was this helpful?