Use Stores with Anthropic's Claude API

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

While Claude 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

import anthropic
import stores

# Initialize Anthropic client
client = anthropic.Anthropic()

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

# Get the response from the model
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "What are the top 10 posts on Hacker News today?"}
    ],
    tools=index.format_tools("anthropic"),  # Format tools for Anthropic
)

# Execute the tool call
tool_call = response.content[-1]
result = index.execute(tool_call.name, tool_call.input)
print(f"Tool output: {result}")

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. Call the model with the tools

Remember to add your Anthropic API key (ANTHROPIC_API_KEY) to your .env file.

To format the tools according to the JSON schema required by the Anthropic API, we will use index.format_tools("anthropic").

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "What are the top 10 posts on Hacker News today?"}
    ],
    tools=index.format_tools("anthropic"),
)

3. Parse the model response

We can then parse response.content to retrieve the tool name and arguments.

response.content
[
    TextBlock(
        citations=None,
        text="I'll help you retrieve the top 10 stories from Hacker News using the get_top_stories function.",
        type="text",
    ),
    ToolUseBlock(
        type="tool_use",
        id="a_random_tool_use_id",
        name="tools-get_top_stories",
        input={
            "num": 10,
        },
    ),
]

Notice how the tool name "tools-get_top_stories" is slightly different from "tools.get_top_stories" documented in the silanthro/hackernews index. Anthropic's API does not support . in function names, so index.format_tools substitutes . with -. The index.execute method in the next step resolves this automatically.

4. Execute the function

Finally, we will use index.execute with the tool name and arguments to run the tool.

tool_call = response.content[-1]
result = index.execute(tool_call.name, tool_call.input)

This gives us the tool call result. You can then supply the result to the model and call the model again to get its final response with the supplied information.

Next steps