# OpenAI Agents SDK

Source: https://docs.tradefloor.dev/openai-agents.html

> Run an OpenAI Agents SDK agent inside a simulated tradefloor market: its decisions become orders, and the run is scored and can be replayed without the model.

`tradefloor.integrations.openai_agents` wraps an OpenAI Agents SDK `Agent`. The adapter runs your agent inside a simulated market on your machine. At each decision tradefloor sends it a JSON observation, checks the decision it returns, places the orders in the simulation and scores the run. [LLM adapters](https://docs.tradefloor.dev/llm-adapters.html) covers [the observation payload](https://docs.tradefloor.dev/llm-adapters.html#the-observation-payload) and [decisions and model calls](https://docs.tradefloor.dev/llm-adapters.html#decisions-and-model-calls) for all the adapters.

## Install

```
pip install "tradefloor[openai-agents]"
```

## A first run

This runs with no API key. The adapter gets `ScriptedModel`, an offline model the SDK ships for testing.

`ScriptedModel` from `agents.testing` answers each call with a function of the input. `payload_of` reads back the observation the adapter sent.

```
import json

import tradefloor as tf
from agents import Agent
from agents.testing import ModelStep, ScriptedModel, assistant_message
from tradefloor.integrations.openai_agents import OpenAIAgentsAdapter, payload_of

def answer(call):
    # reads the same payload a real model is sent
    first = payload_of(call)["assets"][0]
    order = {"symbol": first["symbol"], "side": "BUY", "quantity": 200}
    return [assistant_message(json.dumps({"actions": [order],
                                          "rationale": "add"}))]

offline = ScriptedModel([ModelStep.respond(answer)] * 5)
pm = Agent(name="Portfolio Manager", instructions="You manage a small portfolio.")
adapter = OpenAIAgentsAdapter(pm, mode="live", model=offline)

market = tf.Universe.random(12, seed=4242)
card = tf.evaluate({"pm": adapter}, seed=4242, universe=market, days=5)["pm"]
print(card)
print(len(adapter.record), "decisions")
```

```
Scorecard('pm', pnl=1,621, return=+0.16%, trades=5, impact=+0.38bps, sharpe=n/a (short run), vol=0.7%, in_market=100%, exposure=0.03x)
5 decisions
```

`OpenAIAgentsAdapter` defaults to `mode="replay"`. For a live run, pass `mode="live"` or use `openai_agent(agent)`, which is this adapter with live as its default. Give the `Agent` a model your account can use, set `OPENAI_API_KEY` and leave out `model=`. The SDK has tracing on by default and sends traces to OpenAI, so the adapter turns tracing off for each run it starts unless you pass `tracing=True`.

## Tested with

`openai-agents` 0.23.1 and tradefloor 0.11.0.
