Skip to main content
OpenTools gives you ready-made toolsets (like Trading) that return consistent outputs. In this quickstart, you’ll create a working agent that connects to your Alpaca account and returns your account information.

Get started

1

Install the SDK

Open your terminal. We’re going to create a folder called my_agent, set up a virtual environment, and install the minimal dependencies.

mkdir my_agent
cd my_agent
uv venv
uv pip install opentools-sdk openai python-dotenv
Use a virtual environment (venv) to ensure this demonstration is not affected by your other cool projects.
2

Add your API keys to your .env file

OpenTools reads credentials from environment variables. For this quickstart we’ll use Alpaca because of its strong capabilities and easy setup.

Model API key

If you already have an OpenAI API key, just add it to your newly-created .env file.
  • Navigate to the OpenAI dashboard (API keys section)
  • Click Create new secret key
  • Copy the key and store it somewhere safe (OpenAI recommends a secure place like your .zshrc but ensure to add it to your .env file)
In this demo we use a .env file and python-dotenv.
If you already export OPENAI_API_KEY globally, you can omit it from .env.

Alpaca API key

If you already have Alpaca keys, skip to the .env section below.
  • Create an Alpaca account (no funding required for paper trading)
  • In the dashboard homepage, find Your API Keys
  • Click Generate New Keys
  • Copy:
    • Key ID
    • Secret Key (shown only once)
Alpaca only shows the Secret Key once. If you lose it, you must generate a new key pair.

Create a .env file

This quickstart uses paper trading by default and never places live trades unless you explicitly enable it. OpenTools does not store keys or manage OAuth. All credentials stay on your machine and are only used when tools are called.

In the same folder as main.py, create a file named .env:

OPENAI_API_KEY="your_openai_api_key"
ALPACA_KEY="your_alpaca_key_id"
ALPACA_SECRET="your_alpaca_secret_key"
3

Run your first tool call

You’re about to run a small tool loop: the LLM decides which trading tool to call, OpenTools executes it, and the LLM summarises the result.

This example uses paper trading and minimal output. Nothing places orders unless you explicitly ask it to.

Create a main.py

Create a file named main.py and paste the following code:

import asyncio
import os

from dotenv import load_dotenv
from openai import AsyncOpenAI

from opentools import trading
from opentools.adapters.models.openai import run_with_tools


async def main() -> None:
    # load OPENAI_API_KEY, ALPACA_KEY, ALPACA_SECRET from .env
    load_dotenv()

    # this quickstart uses no framework, just the model
    client = AsyncOpenAI(api_key=os.environ["OPENAI_API_KEY"])

    # add the alpaca tools
    service = trading.alpaca(
        api_key=os.environ["ALPACA_KEY"],
        api_secret=os.environ["ALPACA_SECRET"],
        # specify the model, this is required for bundling tools correctly
        model="openai",
        # specify paper trading account or live trading account
        paper=True,
        # specify minimal output returned to model (maximum token efficiency)
        minimal=True,
    )

    prompt = "Show my account summary and list any open positions."

    # run_with_tools creates the tool-loop to make your development time quicker
    result = await run_with_tools(
        client=client,
        model="gpt-4.1-mini",
        service=service,
        user_prompt=prompt,
    )

    print(result)


if __name__ == "__main__":
    asyncio.run(main())

The code above will add the tools automatically to the LLM context and use a loop to relay tool responses. The tools output structured schemas to ensure interpretability is maximised and relay the information accurately - this has been tested with LLMs as small as 7 billion parameters with clear, correct results.

Run your code

uv run python main.py

If the model responds without calling tools, ensure you structure yours prompts directly around the tool, here are some examples: “Get my account”, “List positions”, or “Show recent orders”.

If you see a KeyError, one of your environment variables is missing or your .env file is not in the same directory as main.py.

What just happened

When you ran main.py, you gave the model a set of tool definitions from the Trading module. The model decided whether to call any tools. OpenTools executed those calls against Alpaca, then fed the results back to the model until it returned your final answer.

This resulted in token-efficient output for your agent and guaranteed structure if calling the same tool again since intperpretability is standardised for model performance. Learn more about our capabilities and project structuring in the cards below.


Next steps

This quickstart uses OpenAI directly. If you want a framework-managed agent loop, using PydanticAI for example, head to Frameworks or navigate by clicking on the card below.

Frameworks

Frameworks provides a quick introduction to deploying established patterns that are supported by OpenTools, like PydanticAI.