Skip to main content
The OpenAI Agents SDK handles tool execution automatically. Use Converter.items_to_messages() to convert to Acontext-compatible format.

Quick Start

acontext create my-agent-project --template-path "python/openai-agent-basic"
Install CLI first: curl -fsSL https://install.acontext.io | sh

Manual Setup

1

Install dependencies

pip install openai-agents acontext python-dotenv
2

Configure environment

OPENAI_API_KEY=your_openai_key_here
ACONTEXT_API_KEY=sk-ac-your-api-key
3

Run agent with Acontext

import asyncio
import os
from agents import Agent, Runner, OpenAIChatCompletionsModel, AsyncOpenAI, function_tool
from agents.models.chatcmpl_converter import Converter
from acontext import AcontextClient

client = AcontextClient(
    api_key=os.getenv("ACONTEXT_API_KEY"),
)

# If you're using self-hosted Acontext:
# client = AcontextClient(
#     base_url="http://localhost:8029/api/v1",
#     api_key="sk-ac-your-root-api-bearer-token",
# )

@function_tool
def get_weather(city: str) -> str:
    """Returns weather info for the specified city."""
    return f"The weather in {city} is sunny"

agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant",
    model=OpenAIChatCompletionsModel(
        model="gpt-4o-mini",
        openai_client=AsyncOpenAI(),
    ),
    tools=[get_weather],
)

async def main():
    session = client.sessions.create()

    # Run agent
    result = await Runner.run(agent, "Plan a 3-day trip to Finland")

    # Continue conversation
    user_msg_2 = {"role": "user", "content": "Check the weather there"}
    new_input = result.to_input_list() + [user_msg_2]
    result = await Runner.run(agent, new_input)

    # Convert and store to Acontext
    messages = Converter.items_to_messages(result.to_input_list())
    for msg in messages:
        client.sessions.store_message(session_id=session.id, blob=msg, format="openai")

    # Extract tasks
    client.sessions.flush(session.id)
    tasks = client.sessions.get_tasks(session.id)

    for task in tasks.items:
        print(f"Task: {task.data.task_description} | Status: {task.status}")

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

Resume Sessions

from helper import message_to_input_items

messages = client.sessions.get_messages(session_id, format="openai")

# Convert back to Responses API format
conversation = []
for msg in messages.items:
    items = message_to_input_items(msg)
    conversation.extend(items)

conversation.append({"role": "user", "content": "Continue"})
result = await Runner.run(agent, conversation)

Next Steps