llm_agents is a small library for building agents controlled by a large language model (LLM), and its design is deeply inspired by langchain. The project's main goal is to help developers better understand how such agents work and to present their core mechanism with a minimal number of lines of code. Compared with langchain, llm_agents avoids excessive files and abstraction layers, choosing instead to build the most important parts of a simple agent from scratch.
Main Capabilities
The agent receives instructions through a prompt, which explains the basic way to use tools to complete tasks. Tools are custom-built components that the agent can use. At present, the project has implemented the following tools: executing Python code in a REPL, using Google search, and searching on Hacker News.
The agent runs in a loop of "Thought -> Action -> Observation -> Thought...". Among these, the thought and action (including the action's input parameters) are generated by the LLM, while the observation results are generated by tools, such as Python print output or the text results of a Google search. In each loop, the LLM receives new information appended to the prompt, enabling it to take the next action based on that information. When the agent has collected enough information, it gives a final answer.
Usage
To install the library locally, you first need to clone its directory, then run the installation command inside the directory (the specific installation command is not clearly stated in the provided material). In addition, the following environment variables need to be provided:
- OPENAI_API_KEY: used to call the OpenAI API (available at https://platform.openai.com/account/api-keys to obtain)
- SERPAPI_API_KEY: used for the Google search tool (available at https://serpapi.com/ to obtain)
You can simply export these variables in bash, for example: export OPENAI_API_KEY='sh-lsdf....'. Then, run the script python run_agent.py to ask your question. To build your own agent, you can refer to the example provided by the project. Of course, you can also build custom tools or omit certain tools; for example, if you do not want to create a SERPAPI key, you can skip the Google search tool.
Applicable Scenarios
This project is suitable for developers, researchers, or enthusiasts who want to gain an in-depth understanding of the internal working mechanism of LLM agents. It provides a minimalist implementation that makes it easy to learn the basic loop logic of agents, how tools are called, and how to interact with the LLM. It is also suitable for scenarios that require rapid prototype validation but do not want to introduce heavier frameworks such as langchain.
Limitations
- The project depends on the OpenAI API and a SerpAPI key, and requires the corresponding API access permissions.
- The number of provided tools is limited, currently including only Python REPL, Google search, and Hacker News search.
- The specific installation command, complete code structure, and more detailed usage steps are not provided in the material and need to be referenced from the repository contents.