How to Intermediate · 3 min read

How to give OpenAI GPT access to tools

Quick answer
To give OpenAI GPT access to tools, build an agent that connects the model with external APIs or functions by passing tool outputs as context in the conversation. Use the OpenAI SDK to orchestrate calls where the model decides when and how to invoke tools, enabling dynamic tool usage within chat completions.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable to authenticate requests.

bash
pip install openai>=1.0

Step by step

Create a simple agent that lets gpt-4o call a tool function (e.g., a calculator) by detecting commands in the prompt and returning results as context for the next model call.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

def calculator_tool(expression: str) -> str:
    try:
        # WARNING: eval used only for demo; use a safe parser in production
        result = eval(expression, {"__builtins__": {}})
        return str(result)
    except Exception as e:
        return f"Error: {e}"

# Initial user prompt asking for a calculation
user_prompt = "Calculate 12 * 8 and tell me the result."

# First, send prompt to GPT to detect tool usage
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": user_prompt}]
)

model_reply = response.choices[0].message.content

# Simple heuristic: extract expression from model reply (demo only)
# In practice, use structured tool calls or function calling
expression = "12 * 8"

# Call the tool
tool_output = calculator_tool(expression)

# Provide tool output back to GPT for final answer
final_response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": user_prompt},
        {"role": "assistant", "content": model_reply},
        {"role": "system", "content": f"Tool output: {tool_output}"}
    ]
)

print(final_response.choices[0].message.content)
output
The result of 12 * 8 is 96.

Common variations

  • Use async calls with asyncio for concurrency.
  • Leverage function_call feature in gpt-4o to define and invoke tools natively.
  • Integrate external APIs (weather, search) by parsing model instructions and returning API results as context.

Troubleshooting

  • If the model ignores tool instructions, ensure your prompt clearly defines tool usage or use the function_call parameter.
  • For errors in tool execution, validate inputs carefully and sanitize expressions.
  • Check API key and environment variable setup if requests fail with authentication errors.

Key Takeaways

  • Use an agent pattern to connect GPT with external tools by passing tool outputs as context.
  • Leverage function_call in gpt-4o for native tool invocation support.
  • Always sanitize and validate inputs when executing tool functions to avoid errors or security risks.
Verified 2026-04 · gpt-4o
Verify ↗