OpenAI Agents SDK integration

What you get

Use OpenAI Agents SDK with KeepBal discovery, inspection and paid tool execution through one key and prepaid wallet.

Prerequisites

Create a KeepBal account and API key. Signup includes $1 credit once. Keep the key private and ensure sufficient wallet credit before executing a tool.

Create an account · API keys

Setup

Install Python and run: pip install openai-agents. Export KEEPBAL_API_KEY and OPENAI_API_KEY. The example uses the SDK model provider separately from KeepBal tool billing.

import asyncio
import os
from agents import Agent, Runner
from agents.mcp import MCPServerStreamableHttp

async def main():
    token = os.environ["KEEPBAL_API_KEY"]
    async with MCPServerStreamableHttp(
        name="KeepBal",
        params={"url": "https://keepbal.com/mcp", "headers": {"Authorization": f"Bearer {token}"}, "timeout": 30},
        cache_tools_list=True,
    ) as server:
        agent = Agent(name="Assistant", instructions="Use KeepBal tools.", mcp_servers=[server])
        result = await Runner.run(agent, "Inspect exa/contents, then fetch https://example.com with text enabled and summarize it.")
        print(result.final_output)

asyncio.run(main())

Keep the server async context open while Runner executes. If tool listing works but calls fail with authentication errors, verify the Authorization header. Check the model-provider key for model errors.

OpenAI Agents SDK official documentation

Worked example: read a web page

Ask your agent: Find the page-content tool, inspect exa/contents, then fetch https://example.com with text enabled and summarize the result. Review the published price before approving execution. Data is from Exa; this does not imply partnership.

search_tools {"query":"page contents"}
inspect_tool {"tool":"exa/contents"}
run_tool {"tool":"exa/contents","input":{"body":{"urls":["https://example.com"],"text":true}}}

These are MCP tool names and argument objects. The client wraps them in tools/call requests. If run_tool returns pending with a run_id, call get_run {"run_id":"RETURNED_RUN_ID"}; do not repeat run_tool.

Plain HTTP tool wrapper

Alternatively, install requests with pip install requests and register this wrapper. It sends POST https://keepbal.com/apis/v1/exa/contents with the direct JSON body, without the MCP input envelope. Choose either this wrapper or the MCP tools for the example.

import os
import requests
from agents import function_tool

@function_tool
def fetch_page(url: str) -> dict:
    """Fetch page contents through KeepBal, billed per successful call."""
    response = requests.post(
        "https://keepbal.com/apis/v1/exa/contents",
        headers={"Authorization": f"Bearer {os.environ['KEEPBAL_API_KEY']}"},
        json={"urls": [url], "text": True},
        timeout=60,
    )
    response.raise_for_status()
    return response.json()

from agents import Agent, Runner
agent = Agent(name="Reader", tools=[fetch_page])
result = Runner.run_sync(agent, "Fetch https://example.com and summarize it.")
print(result.final_output)

Troubleshooting

Initialization and tools/list work without a key; every tools/call requires a valid bearer key, including search and inspect. For 401 check the key and client launch environment. For insufficient_credits check Wallet; for rate_limited respect Retry-After. Check Usage after uncertain outcomes before retrying a paid call. Truncated or unsupported MCP results must not be retried.

Exa contents tool · Search and extraction tools · Errors · MCP reference · All integrations