Documentation / Integrations

Run bounded OpenAI agents

04 - Build · 02 - Get started

Two independent agents can assess eligibility and credit before another recommends a loan. After a status update, rerunning all three agents repeats unaffected work; retaining all three keeps stale reasoning. Use the adapter to capture complete accessible inputs and publish each successful run, then have the application explicitly select affected agents from Co2 plans.

Install and run

CPython 3.11–3.14 and the existing Rust/Maturin prerequisites are described in the Python guide. From the repository root:

python -m venv .venv
source .venv/bin/activate
python -m pip install -e '.[openai-agents]'
python examples/openai_agents_lending.py
python -m unittest discover -s agent_frameworks_tests/openai_agents -v

The openai-agents extra pins openai-agents==0.20.0. This version works with the existing llm-example extra's openai<3 constraint; The pinned SDK and application constraints must be resolved together; upgrading an optional framework is a separate compatibility decision. The SDK's newer documented agents.testing.ScriptedModel is unavailable in 0.20.0.

The mock uses the public Model interface while exercising the actual async Runner.run and structured output parsing. These version choices do not add OpenAI dependencies to the base SDK. The example and adapter were exercised on CPython 3.14 on the documented hosts; see the configuration matrix rather than inferring every combination from package metadata.

The mock uses synthetic values and makes no model calls or external financial operations. A live experiment is explicitly opt-in, uses at most six model calls, a 30-second client timeout, zero OpenAI client retries, max_turns=1, a 256-token output limit, and per-run tracing disabled. The example's model wrapper also enforces its six-call budget. Existing environment variables take precedence over .env; never print or commit credentials.

python examples/openai_agents_lending.py --live --model gpt-4.1-mini --env-file .env

gpt-4.1-mini is configurable. Verify model access and pricing against the intended provider environment and model documentation. The output token limit bounds output per request, not provider charges in all failure scenarios.

Map executions and artifacts

Each OpenAIAgentsAdapter owns a fresh random session namespace around a Coherence instance. agent is a host-supplied stable logical name mapped to one Co2 computation ID; it need not equal the SDK Agent.name. Each run_agent invocation gets a fresh Co2 attempt ID; a successful output encoder produces bytes for a new immutable artifact and AgentResult returns the session, logical agent, computation, attempt, artifact and Co2 record. A replacement has a new attempt and artifact. Namespaces prevent collisions even when sessions share one authority. This session ID is an in-memory binding, not an Agents SDK session ID, checkpoint, credential or authorization token.

Register each resource and authoritative version with register_resource. Call run_agent with the full set of accessible host resource versions and upstream (logical_agent, artifact_id) pairs. The input builder receives the public ScopedContext and Invocation; the host owns the data associated with resource observations.

The adapter validates the expected versions against Co2's captured snapshot, calls the public asynchronous Runner.run, encodes only a successfully completed final_output, and publishes through complete_outputs. Co2 atomically rejects late or superseded dependencies. A failed model, failed encoder, cancellation, stale result or invalid declaration publishes no artifact for that failed call.

The adapter abandons its captured attempt and burns unused automatically issued identities; direct SDK completion has separate pending-attempt rules.

The default strategy remains invocation-level and the default execution kind remains UNKNOWN, which is rejected for execution. Classify model work as PROBABILISTIC_REASONING; it is reusable when valid and blocked when stale unless the host explicitly passes PlanPolicy(allow_probabilistic_recomputation=True). The adapter checks Co2's plan but never computes validity or automatically schedules recomputation. plan and reuse are snapshots; callers must react to later changes. record_historical registers only a host-provided human/effect/irreversible record and refuses repetition; it neither executes nor proves the historical operation.

The lending example starts two independent Runner.run calls with asyncio.gather, records their artifacts, derives the recommendation, records synthetic historical approval and funding, and changes status. The default plan blocks stale probabilistic work. Under the explicit policy the host verifies RECOMPUTE for eligibility/recommendation, REUSE for credit, REVALIDATE for approval and ESCALATE for funding, reuses credit and explicitly runs the other two agents.

Its separate late call is released after a concurrent update. Counts are instrumented at the public model boundary, not derived from the plan. In live mode, a very fast late result may finish before the update and be reported as inconclusive.

Keep host responsibilities explicit

The adapter uses public Runner APIs and public agent output types; see also the SDK's results documentation. These links describe the current SDK, which may differ from the pinned version.

Example outcome

The default synthetic workflow records eligibility 2, credit 1, recommendation 2, plus a separate deliberately late attempt. These are executed mock-boundary counts, not billing or live-provider performance measurements. Credit retains its artifact; replacements get new attempt/artifact identities. Stale approval requires REVALIDATE; irreversible funding requires ESCALATE and funding execution stays zero. The host explicitly enables probabilistic recomputation for the reasoning demonstration. Without that policy, stale reasoning blocks.

Check versions, complete accessible context, output schema and current artifact bindings before using results. Session IDs correlate process-local state; they are not authorization credentials. Rejected late results need fresh current context and new identities when eligible. A source update after a successful check can still occur; no adapter locks external source systems. See execution semantics, failure handling and lifecycle cleanup.