0

OpenAI Provider

OpenAI Provider explains the cloud layer of MTPX with practical guidance for building inspectable, tool-using agents.

Op
OpenAI Provider
Chapter 038ProvidersCloudIndex
01Orientation

OpenAI Provider belongs to the providers track. The page breaks the idea into responsibilities, implementation rules, failure modes, and the signals you should expose in a product UI.

Use this when

Use this when you need to understand how openai provider affects a real MTPX agent before you wire it into an application.

  • What openai provider owns in the runtime.
  • How it connects to planning, tool execution, events, providers, or storage.
  • What to log, test, and expose to users when this layer is active.
  • Common mistakes that make agent systems hard to inspect.
02 / syntax starter
from mtp import Agent
from mtp.providers import Groq

agent = Agent.MTPAgent(
    provider=Groq(model="llama-3.3-70b-versatile"),
    tools=tools,
)
03 / manual

Read the system

The complete source manual—syntax, examples, linked references, edge cases, and implementation notes.

Documentation index ↗

OpenAI provides GPT-4o, GPT-4, and GPT-3.5-turbo with native tool/function calling support.

Install

bashpip install "mtpx[openai]"

Or install the SDK directly:

bashpip install openai

API Key Setup

  1. Install dotenv support:
bash   pip install python-dotenv

Or with the MTP extra:

bash   pip install "mtpx[dotenv]"
  1. Create a .env file in your project root:
text   OPENAI_API_KEY=sk_your_key_here
  1. Load it in your code before creating the provider:
python   from mtp import Agent

   Agent.load_dotenv_if_available()  # reads .env file

Get an API key at platform.openai.com.

Option 2: System environment variable

bash# Linux/macOS
export OPENAI_API_KEY="sk-..."

# Windows PowerShell
$env:OPENAI_API_KEY="sk-..."

Quick Start

pythonfrom mtp import Agent
from mtp.providers import OpenAI

Agent.load_dotenv_if_available()  # loads OPENAI_API_KEY from .env

provider = OpenAI(model="gpt-4o")
tools = Agent.ToolRegistry()
agent = Agent(provider=provider, tools=tools)

reply = agent.run_loop("What is 25 * 4 + 10?")
print(reply)

Parameters

ParameterTypeDefaultDescription
modelstr"gpt-4o"OpenAI model ID
api_keystr | NoneNoneAPI key (falls back to OPENAI_API_KEY env var)
temperaturefloat0.0Sampling temperature
tool_choicestr | dict"auto"Tool selection: "auto", "none", "required", or {"type": "function", "function": {"name": "..."}}
parallel_tool_callsboolTrueAllow parallel tool calls
clientAny | NoneNonePre-configured openai.OpenAI client instance

Capabilities

CapabilityValue
Tool callingYes
Parallel tool callsYes (configurable)
Input modalitiestext, image, audio, file
StreamingFallback
Usage metricsRich
Reasoning metadataNo
Native asyncNo (uses thread fallback)
  • gpt-4o — Best overall tool calling and multimodal (default)
  • gpt-4o-mini — Fast, cheaper, good tool support
  • gpt-4-turbo — Strong reasoning
  • gpt-3.5-turbo — Fastest, cheapest

Full Example

pythonfrom mtp import Agent
from mtp.providers import OpenAI

Agent.load_dotenv_if_available()

provider = OpenAI(
    model="gpt-4o",
    temperature=0.0,
    tool_choice="auto",
    parallel_tool_calls=True,
)

tools = Agent.ToolRegistry()
agent = Agent(provider=provider, tools=tools, debug_mode=True)

reply = agent.run_loop(
    "Calculate (25 * 4) + 10 and list files in the current directory.",
    max_rounds=4,
    tool_call_limit=12,
)
print(reply)

Rate Limit Tracking

The OpenAI provider automatically extracts rate limit headers (x-ratelimit-*, retry-after) from responses and includes them in action metadata.

Source

src/mtp/providers/openai_provider.py

01

Read

Understand where OpenAI Provider sits in the agent loop before adding abstractions.

02

Wire

Connect the smallest useful provider, registry, store, or event stream first.

03

Observe

Expose events, logs, results, and failure states while the runtime is still moving.

04

Harden

Add policy, tests, retries, and audit traces after the behavior is visible.

Next docOpenRouter Provider