Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 120 additions & 1 deletion examples/decisions/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,123 @@
# Decisions API recipes
# Decisions in the Python Agents SDK

## Ask a typed question

Set `Agent.decisions` once. Each call sends only the question, explicit input and requested output schema. This example makes paid requests when invoked.

```python
from typing import Literal

from pydantic import BaseModel

from agents import Agent, AgentHooks, OpenAIDecisions, Runner
from agents.decorators import tool
from agents.tool_context import ToolContext


class Ticket(BaseModel):
urgent: bool
team: Literal["billing", "support"]


@tool
async def classify(ctx: ToolContext[None], report: str) -> str:
return await ctx.decide("Which team?", input=report, choices=["billing", "support"])


async def check(run, proposal):
ready = await proposal.decide("Does the reply include a next step?", input=proposal.output)
return "finish" if ready else "continue"


async def answer_ticket(text: str):
async with OpenAIDecisions() as provider:
agent = Agent(
name="Help",
decisions=provider,
tools=[classify],
hooks=AgentHooks(final_output_policy=check),
)
ticket = await agent.decide("Classify this request.", input=text, output_type=Ticket)
reply = await Runner.run(agent, text, max_turns=3)
return ticket, reply.final_output
```

Without `choices` or `output_type`, the result is a boolean. The built-in provider uses a probability threshold of 0.5. Choose one output form per call. Supported types include finite scalar choices, required nested records, integers spanning up to 255 values and floats with integer bounds up to 9 apart. Unsupported schemas fail before dispatch. Input can be text or model input items. The SDK adds no conversation history or instructions.

Within one run, a handoff target inherits the root provider unless it declares its own. Nested runs, including agents as tools, start with their own provider. Within a function tool, a model input filter or a final output policy, call `decide` on the live `ToolContext`, `CallModelData` or `ProposedFinalOutput` respectively. Use `provider=...` for a single call or implement `DecisionProvider.get_decision` for another backend. An answer grants no permissions.

Use `async with OpenAIDecisions()` or call `aclose()` for the default client. With `OpenAIDecisions(client=client)`, the caller owns the client. The built-in adapter needs an OpenAI client version with Decisions support and disables retries. Decision calls and tokens are outside Runner usage and budgets. With tracing enabled, the `decision.usage` span records available metadata without the question, input or answer. Missing usage is unknown.

## Let the primary model request a specialist

Use `agents.decisions.decision_handoff` when the primary model should decide **when** to transfer. Decisions chooses **which** specialist from the labels your application currently allows. If the primary model answers directly, there is no Decisions request.

[Run the offline comparison](compare_handoffs.py) from the repository root without credentials.

```bash
uv run python examples/decisions/compare_handoffs.py
```

The example compares direct and Decisions handoffs on the same three labeled inputs. Both paths use the same scripted specialists and settings. Route accuracy measures the fraction routed correctly. Task completion measures the fraction with the expected final answer. Each row also reports total elapsed time inside Runner and request counts. Estimated cost is unknown (`null`). The predictions include deliberate mistakes. These fixtures check the workflow and scoring. They provide no evidence of live accuracy, latency or cost.

For live requests, configure a provider on the source agent. This function makes paid calls when invoked.

```python
from openai import AsyncOpenAI

from agents import Agent, OpenAIDecisions, Runner
from agents.decisions import decision_handoff


async def answer(client: AsyncOpenAI, text: str):
source = Agent(
name="Triage",
instructions="Transfer requests that need a specialist.",
decisions=OpenAIDecisions(client=client),
)
targets = {
"billing": Agent(name="Billing", instructions="Help with invoices."),
"account": Agent(name="Account", instructions="Help with account settings."),
}
route = decision_handoff(
source,
question="Which team can help?",
targets=targets,
)
source.handoffs.append(route)
return await Runner.run(source, text)
```

Use a client with Decisions support. The caller owns its lifetime. Give each clone its own handoff list and selector. For one fixed target, use an ordinary `handoff(target)`.

- **Evidence.** The model supplies one nonblank `input` string of up to 8,192 UTF-8 bytes. The provider receives that string, the question and the allowed choices. The model can copy sensitive text into the string. The specialist receives the usual handoff history. Set the handoff's `input_filter` when the specialist needs less context.
- **Authority.** The handoff belongs to the source agent. Labels bind to the original agent objects and names. Removing, replacing or renaming a target makes it unavailable. Use a synchronous, side-effect-free `is_available(ctx, label)` to check application permissions. The handoff checks the selected route again before transfer. Runner keeps the run context, call IDs, tool restrictions and approvals.
- **Failures.** Refusals and invalid answers raise `DecisionError`. Revoked routes raise `ValueError`. The provider deadline defaults to 30 seconds. On timeout or cancellation, the SDK waits for provider cleanup and discards late answers. Timeout raises `asyncio.TimeoutError`. If the provider cancels its own request, the SDK raises `RuntimeError`. Transport errors propagate. There is no automatic fallback or retry. To return an error to the primary model, explicitly set the handoff's `failure_error_function`. For immediate stream cancellation, call `result.cancel()` and drain `result.stream_events()`.
- **Error privacy.** When either model or tool data logging is disabled, the handoff removes routing traceback frames and exception chains before propagating a failure. Provider errors and metadata can still contain sensitive data. Keep custom error messages and recovery callbacks safe to share.
- **Scope.** Both Runner modes support specialist selection. Decisions cannot invoke a tool, grant permissions or tell the source to continue. Realtime and dynamic agent factories are outside this integration.
- **Available labels.** The model sees the handoff only when 2 to 255 labels are available. If the count falls outside that range before invocation, the handoff raises `ValueError` without asking the provider or transferring. The SDK never transfers by default.

### Resume a specialist after a tool approval

The handoff registers its targets for `RunState` automatically. Save `result.to_state().to_string()` when the run pauses. Protect the snapshot from edits. Restore only trusted snapshots with the same source, handoff names, labels and specialists. When rebuilding the graph, you authorize the agents, tools and permission checks you supply under those names.

If you use `is_available`, pass fresh application permissions through `context_override`. The callback should read them from `ctx.context`. Saved permissions alone cannot authorize the active specialist. For a nested `Agent.as_tool()` run, pass the override when restoring the outer run. The nested specialist shares the application context and keeps its own approvals.

```python
from agents import RunState

state = await RunState.from_string(source, saved, context_override=current_context)
```

Use a separate specialist instance for each label and ordinary handoff. A saved specialist identifies one route. Loading and resuming recheck each selected label on the active handoff path, including ordinary descendants and pending agent tools. Returning to an earlier agent ends the branch below it. Missing bindings or denied access stop the resume before dispatch. Decisions does not run again. Review each pending call in `state.get_interruptions()`, then approve or reject it and resume with `await Runner.run(source, state)`.

Keep the graph and grants current. If selection fails after a tool has run, keep the updated `RunState` and inspect completed results before retrying.

### Evaluate routing separately

Compare ordinary and Decisions handoffs on held-out labels with the same specialists. Record wrong transfers, refusals, end-task outcomes, latency and any available usage. The offline comparison cannot establish which route is better for your workload.

## Direct client recipes

These examples compose the native `AsyncOpenAI.decisions.create()` client with existing Agents SDK APIs. They do not add a Decisions model adapter or a generic risk policy.

Expand Down
103 changes: 103 additions & 0 deletions examples/decisions/compare_handoffs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""Compare two scripted handoff paths on labeled fixtures without making API calls."""

import asyncio
import json
from time import perf_counter

import httpx2
from openai import AsyncOpenAI

from agents import Agent, RunConfig, Runner, handoff
from agents.decisions import OpenAIDecisions, decision_handoff
from agents.testing import ScriptedModel, assistant_message, function_call

# Each row records the input, expected route, expected answer and two scripted predictions.
CASES = (
("Where can I reset my password?", "account", "Open settings.", "account", "account"),
("Where is my invoice?", "billing", "Open invoices.", "account", "billing"),
("How do I change my password?", "account", "Open settings.", "account", "billing"),
)


async def compare() -> None:
predictions = {text: predicted for text, _, _, _, predicted in CASES}
calls = 0

def respond(request: httpx2.Request) -> httpx2.Response:
nonlocal calls
calls += 1
choice = predictions[json.loads(request.content)["input"]]
return httpx2.Response(
200,
json={
"answers": [
{
"type": "choice",
"name": "decision",
"choice": choice,
"confidence": 1.0,
"probabilities": [
{"value": c["value"], "probability": float(c["value"] == choice)}
for c in json.loads(request.content)["questions"][0]["choices"]
],
}
]
},
)

async with AsyncOpenAI(
api_key="synthetic",
base_url="https://fixture.invalid/v1",
http_client=httpx2.AsyncClient(transport=httpx2.MockTransport(respond), trust_env=False),
) as client:
for path in ("primary", "decisions"):
calls = 0
correct = completed = requests = 0
seconds = 0.0
for text, label, expected, primary, _ in CASES:
targets = {
name: Agent[None](name=name, model=ScriptedModel([[assistant_message(answer)]]))
for name, answer in (
("account", "Open settings."),
("billing", "Open invoices."),
)
}
source = Agent[None](name="Triage")
if path == "primary":
source.handoffs.extend(
handoff(agent, tool_name_override=name) for name, agent in targets.items()
)
call = function_call(primary, {}, call_id="transfer")
else:
source.decisions = OpenAIDecisions(client=client)
route = decision_handoff(
source, question="Which team can help?", targets=targets
)
source.handoffs.append(route)
call = function_call(route.tool_name, {"input": text}, call_id="transfer")
source.model = ScriptedModel([[call]])
start = perf_counter()
result = await Runner.run(source, text, run_config=RunConfig(tracing_disabled=True))
seconds += perf_counter() - start
correct += result.last_agent is targets[label]
completed += result.final_output == expected
requests += len(result.raw_responses)
print(
json.dumps(
{
"path": path,
"cases": len(CASES),
"route_accuracy": correct / len(CASES),
"task_completion": completed / len(CASES),
"elapsed_seconds": seconds,
"model_requests": requests,
"decision_requests": calls,
"estimated_cost": None,
"fallback": None,
}
)
)


if __name__ == "__main__":
asyncio.run(compare())
12 changes: 12 additions & 0 deletions src/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
from .agent_output import AgentOutputSchema, AgentOutputSchemaBase
from .apply_diff import apply_diff
from .computer import AsyncComputer, Button, Computer, Environment
from .decisions import (
DecisionError,
DecisionProvider,
DecisionRequest,
DecisionResponse,
OpenAIDecisions,
)
from .editor import ApplyPatchEditor, ApplyPatchOperation, ApplyPatchResult
from .exceptions import (
AgentsException,
Expand Down Expand Up @@ -380,6 +387,11 @@ def enable_verbose_stdout_logging() -> None:


__all__ = [
"DecisionError",
"DecisionProvider",
"DecisionRequest",
"DecisionResponse",
"OpenAIDecisions",
"Agent",
"AgentBase",
"AgentToolStreamEvent",
Expand Down
Loading
Loading