Use Stores with LlamaIndex Agent

In this quickstart, we will be creating a simple agent that can get the top posts on Hacker News.

While large language models can generate text, they need additional tools to perform actions like fetching data from Hacker News. Using Stores, we will add tools for querying the Hacker News API.

Hacker News agent

from llama_index.core.agent import AgentRunner
from llama_index.core.tools import FunctionTool
from llama_index.llms.google_genai import GoogleGenAI

import stores

# Load the Hacker News tool index
index = stores.Index(["silanthro/hackernews"])

# Initialize the LlamaIndex agent with tools
llm = GoogleGenAI(model="models/gemini-2.0-flash-001")
tools = [
    FunctionTool.from_defaults(fn=fn) for fn in index.tools
]  # Use LlamaIndex FunctionTool
agent = AgentRunner.from_llm(tools, llm=llm, verbose=True)

# Get the response from the agent. The LlamaIndex agent will automatically execute
# tool calls and generate a response.
response = agent.chat("What are the top 10 posts on Hacker News today?")
print(f"Assistant response: {response}")

Agent script walkthrough

1. Load the tools

First, we will load the Hacker News tools from the silanthro/hackernews tool index.

index = stores.Index(["silanthro/hackernews"])

You can also load a tool index from a public GitHub repository or load your own custom tools from your repository. Learn more about what a tool index is here.

The Hacker News API doesn't require any API key. If a tool requires an API key, you can pass it via the env_var parameter.

2. Initialize the agent with the tools

Remember to add your Gemini API key (GOGGLE_API_KEY) to your .env file.

We will use LlamaIndex's FunctionTool to wrap the tools from index.tools.

llm = GoogleGenAI(model="models/gemini-2.0-flash-001")
tools = [FunctionTool.from_defaults(fn=fn) for fn in index.tools]
agent = AgentRunner.from_llm(tools, llm=llm, verbose=True)

3. Run the agent

The LlamaIndex agent will automatically execute any functions required by the input task and generate a response with the tool call result.

response = agent.chat("What are the top 10 posts on Hacker News today?")

Next steps