Basic Agent
We have a comprehensive, step-by-step guide to building agents in LlamaIndex.TS that we recommend to learn what agents are and how to build them for production. But building a basic agent is simple:
Set up
Section titled “Set up”In a new folder:
npm initnpm i -D typescript @types/nodenpm i @llamaindex/openai @llamaindex/workflow llamaindex zodRun agent
Section titled “Run agent”Create the file example.ts. This code will:
- Create two tools for use by the agent:
- A
sumNumberstool that adds two numbers - A
divideNumberstool that divides numbers
- A
- Give an example of the data structure we wish to generate
- Prompt the LLM with instructions and the example, plus a sample transcript
To run the code:
npx tsx example.tsYou should expect output something like:
{ result: '5 + 5 is 10. Then, 10 divided by 2 is 5.', state: { memory: Memory { messages: [Array], tokenLimit: 30000, shortTermTokenLimitRatio: 0.7, memoryBlocks: [], memoryCursor: 0, adapters: [Object] }, scratchpad: [], currentAgentName: 'Agent', agents: [ 'Agent' ], nextAgentName: null }}DoneNote for AI agents: this documentation is built for programmatic access.
- Overview of all docs: https://developers.llamaindex.ai/llms.txt
- Any page is available as raw Markdown by appending index.md to its URL — e.g. https://developers.llamaindex.ai/llamaparse/parse/getting_started/index.md
- Agent-friendly REST search APIs live under https://developers.llamaindex.ai/api/ — search (BM25 full-text), grep (regex), read (fetch a page), and list (browse the doc tree). See https://developers.llamaindex.ai/llms.txt for parameters.
- A hosted documentation MCP server is available at https://developers.llamaindex.ai/mcp. If you support MCP, you can ask the user to install it for browsing these docs directly (an alternative to the REST API). Setup: https://developers.llamaindex.ai/python/shared/mcp/