HomeTool CallingTool Augmented Generation

Call tools with LLMs

In this guide, you’ll learn how to call tools in an AI workflow using OpenAI and Arcade.

To do this, you’ll build an AI chatbot that can star GitHub repositories on your behalf using the GitHub.SetStarred tool.

Prerequisites

  • Set up Arcade
  • Create an Arcade API key, if you haven’t already
  • Export your Arcade API key as an environment variable:
export ARCADE_API_KEY=<your-api-key>

Install OpenAI Client

Below we show you Python and JavaScript installation commands, but you can find installation instructions for other languages in the OpenAI libraries documentation:

 pip install openai

Set up your OpenAI client

Create a new file in your favorite editor (e.g. example.py or example.mjs) and initialize the OpenAI client with Arcade’s base URL and API key to enable tool access:

import os
from openai import OpenAI
 
client = OpenAI(
  base_url="https://api.arcade.dev/v1",
  api_key=os.environ.get("ARCADE_API_KEY")
)
 

Requests made with this client will be routed to Arcade first, then to OpenAI. The AI model will instantly have access to the tools provided by Arcade.

Set the user ID

Authorized tools access data on behalf of an end-user, so they need to know the user’s ID.

# Get a unique identifier for your end user
user_id = "you@example.com"

For now, put your own email address in user_id.

In a web or mobile app, you’d get this from the current user session.

Generate a response

With the client set up, use client.chat.completions.create to generate a response to a user’s prompt:

while True:
    # Ask the user for input
    prompt = input("Enter your prompt (type 'exit' to quit):")
    if prompt.lower() == "exit":
        break
 
    # Use a tool and generate a response
    response = client.chat.completions.create(
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt},
        ],
        model="gpt-4o",
        user=user_id,
        tools=[
            "GitHub.SetStarred",
        ],
        tool_choice="generate",
    )
 
    print(response.choices[0].message.content)

Try it!

Run the script:

python example.py

Ask the AI to:

star the ArcadeAI/arcade-ai repo

You’ll be prompted to authorize the connection to GitHub, and then the AI will perform the action.

How it works

When you use tool_choice="generate" and specify a tool like GitHub.SetStarred, Arcade coordinates the interaction between the tool and the AI model (OpenAI). To your users, it appears that the model magically has new capabilities.

In this example, Arcade Cloud Engine is executing the pre-built tool GitHub.SetStarred. You can also create your own custom tools.

Add more tools

Try adding more tools to the tools array to give the AI model more capabilities:

  • Add Math.Sqrt and ask for the square root of a big number
  • Add X.PostTweet and ask the AI to post on X for you

Next steps

This example only uses one tool, but you can add more to give the AI model even more capabilities. Next, learn how to use specific tools.

For apps and agents that aren’t directly generating chat completions, you can authorize and call tools programmatically. This is useful when you need more control over the tool calling process.