Skip to main content

enterprise_ai.runtime.runner

Source: sdks/python/src/enterprise_ai/runtime/runner.py. Back to the Python SDK reference index.

note

This page is generated by tools/docs/gen_sdk_reference.py from the SDK sources and the OpenAPI text folders. Do not edit it by hand: change the source and regenerate.

Model TurnResult​

Outcome of :meth:AgentRunner.run_turn.

Fields:

NameTypeRequiredDescription
final_textstr | NoneyesFinal answer of the model, or None when the turn stopped before one.
messageslist[dict[str, Any]]yesEvery message of the turn, including the tool results and the nudges.
stepsintyesNumber of model calls of the turn.
stop_reasonstryesWhy the turn ended: completed, stopped, waiting_approval, no_progress, max_steps, max_tool_calls, max_cost or max_duration.
nudgesdict[str, int]noNumber of times each continuation rule fired, by rule code.
usagedict[str, Any]noUsage counters added to the budget consumption, for example input_tokens, output_tokens.
tool_callsintnoNumber of tool calls of the turn.

Attributes:

NameTypeRequiredDescription
okboolyesTrue when stop_reason is completed.
outputstr | NoneyesFinal answer text (same as final_text).

TurnResult.state​

state() -> dict[str, Any]

Serializable state stored in checkpoints (resume input).

This element takes no parameter.

Returns: dict[str, Any]. Dict with messages, step, tool_calls, nudges, usage, stop_reason and final.

Class AgentRunner​

Run agent turns against the platform gateway with tools, rules and governance.

Attributes:

NameTypeRequiredDescription
agent_idyesAgent identifier sent in the eai extension.
approval_foryesGlob patterns of tool names that need a human approval.
approval_timeoutyesMaximum wait for an approval decision, in seconds.
audit_modeyesAudit turn with a longer exploration allowance.
budgetyesLimits of the turn (LoopBudget).
clientyesEnterprise AI client (gateway, tools, runs, approvals), or None.
data_classificationyesClassification of the data of the turn, sent in the eai extension.
effortyesReasoning effort of the gateway model calls.
heartbeatyesHeartbeats are posted before every model and tool call.
last_heartbeatHeartbeatResponse | NoneyesLast heartbeat answer, parsed as a HeartbeatResponse, or None.
mcp_toolsyesOpenAI tool specs of the MCP tools.
modelyesModel identifier used for the gateway calls.
on_eventyesEvent callback (kind, data), or None.
read_only_patternsyesGlob patterns of read only tool names.
remote_toolyesFunction that runs the tools that are neither local nor MCP, or None.
remote_toolsyesOpenAI tool specs of the Tool Gateway tools.
require_deliveryyesThe turn must use at least one tool before answering.
rulesyesContinuation rules evaluated by the turn.
run_idyesServer run that receives the steps, checkpoints and heartbeats.
session_idyesSession identifier sent in the eai extension.
stop_eventyesthreading.Event that stops the turn at its next boundary.
subagentsyesSubagent manager plugged into the turn, or None.
task_idyesTask identifier sent in the eai extension.
toolsdict[str, LocalTool]yesLocal tools by name.
verify_patternsyesGlob patterns of verification tool names.
wait_for_approvalyesThe turn waits for approval decisions instead of stopping.

Constructor AgentRunner​

AgentRunner(client: Any = None, model: str | None = None, model_call: ModelCall | None = None, effort: str | None = None, tools: dict[str, LocalTool] | list[LocalTool] | None = None, remote_tools: list[dict[str, Any]] | None = None, mcp_tools: list[dict[str, Any]] | None = None, remote_tool: Callable[[str, dict[str, Any]], Any] | None = None, budget: Any = None, rules: list[ContinuationRule] | None = None, agent_id: str | None = None, session_id: str | None = None, run_id: str | None = None, task_id: str | None = None, data_classification: str | None = None, approval_for: list[str] | None = None, wait_for_approval: bool = True, approval_timeout: float = 3600.0, stop_event: threading.Event | None = None, heartbeat: bool = True, require_delivery: bool = False, audit_mode: bool = False, verify_patterns: tuple[str, ...] = VERIFY_PATTERNS, read_only_patterns: tuple[str, ...] = READ_ONLY_PATTERNS, subagents: Any = None, on_event: Callable[[str, dict[str, Any]], None] | None = None) -> None

Configure the runner; it needs a model_call, or a client and a model.

NameTypeDefaultRequiredDescription
clientAnyNonenoParent client.
modelstr | NoneNonenoModel identifier used for the gateway calls when no model_call is given.
model_callModelCall | NoneNonenoFunction (messages, tools) -> dict that replaces the gateway call.
effortstr | NoneNonenoReasoning effort of the gateway model calls.
toolsdict[str, LocalTool] | list[LocalTool] | NoneNonenoLocal tools, as a dict by name or a list of LocalTool.
remote_toolslist[dict[str, Any]] | NoneNonenoOpenAI tool specs of the Tool Gateway tools offered to the model.
mcp_toolslist[dict[str, Any]] | NoneNonenoOpenAI tool specs of MCP tools, run through client.mcp.call_tool.
remote_toolCallable[[str, dict[str, Any]], Any] | NoneNonenoFunction (name, arguments) that runs the tools that are neither local nor MCP.
budgetAnyNonenoLimits of the turn (max_steps, max_tool_calls, max_cost, max_duration); a default LoopBudget when omitted.
ruleslist[ContinuationRule] | NoneNonenoContinuation rules; default_rules() when omitted.
agent_idstr | NoneNonenoAgent identifier sent in the eai extension of the model calls.
session_idstr | NoneNonenoSession identifier sent in the eai extension.
run_idstr | NoneNonenoServer run that receives the steps, checkpoints and heartbeats; without it nothing is reported.
task_idstr | NoneNonenoTask identifier sent in the eai extension.
data_classificationstr | NoneNonenoClassification of the data of the turn, sent in the eai extension.
approval_forlist[str] | NoneNonenoGlob patterns of tool names (also matched as tool:<name>) that need a human approval.
wait_for_approvalboolTruenoWait for the approval decision; when False, the turn ends with stop_reason waiting_approval (default True).
approval_timeoutfloat3600.0noMaximum wait for an approval decision, in seconds (default 3600.0).
stop_eventthreading.Event | NoneNonenothreading.Event that stops the turn at its next boundary when set; a new one by default.
heartbeatboolTruenoPost a heartbeat to the run before every model and tool call and stop on action=stop (default True).
require_deliveryboolFalsenoSend the model back to work when it answers without using any tool (default False).
audit_modeboolFalsenoAudit turn: allows 8 read only rounds before the exploration nudge instead of 4 (default False).
verify_patternstuple[str, ...]VERIFY_PATTERNSnoGlob patterns of verification tool names (*test*, *verify*, *check*, *lint*).
read_only_patternstuple[str, ...]READ_ONLY_PATTERNSnoGlob patterns of read only tool names (read*, get*, list*...), used by the exploration and verification rules.
subagentsAnyNonenoSubagent manager whose inject_results adds finished subagent results to the messages before each model call.
on_eventCallable[[str, dict[str, Any]], None] | NoneNonenoFunction (kind, data) called on model, tool, nudge, approval_requested and stop events.

AgentRunner.approve​

approve(name: str, arguments: dict[str, Any], approval_id: str | None = None) -> bool

Ask for a human approval of a tool call (tools:invoke) and wait for the decision.

NameTypeDefaultRequiredDescription
namestryesName of the tool to approve.
argumentsdict[str, Any]yesArguments of the tool call, recorded as the reason of the request (first 500 characters).
approval_idstr | NoneNonenoExisting approval request to wait for; a new one is created when omitted.

Returns: bool. True when the decision is approved; without a client or with wait_for_approval=False the turn stops as waiting_approval.

AgentRunner.call_model​

call_model(messages: list[dict[str, Any]], tools: list[dict[str, Any]]) -> dict[str, Any]

Call the model with model_call, or else through client.gateway.chat with the model, effort and eai of the runner.

NameTypeDefaultRequiredDescription
messageslist[dict[str, Any]]yesChat messages of the turn so far.
toolslist[dict[str, Any]]yesOpenAI tool specs offered to the model.

Returns: dict[str, Any]. Raw chat.completion dict, with eai_cost when the gateway measured the cost; raises ValueError without model_call, client or model.

Raises: ValueError.

AgentRunner.check_stop​

check_stop(step: int) -> None

Raise when the stop event is set or the heartbeat answers action=stop.

NameTypeDefaultRequiredDescription
stepintyesCurrent step number, sent with the heartbeat.

Returns: None.

AgentRunner.checkpoint​

checkpoint(state: dict[str, Any]) -> Any

Write a checkpoint of the turn on the run, labelled step-<n>.

NameTypeDefaultRequiredDescription
statedict[str, Any]yesTurn state to store (see TurnResult.state).

Returns: Any. Answer of client.runs.checkpoint, or None without a client or a run.

AgentRunner.eai​

eai() -> dict[str, Any]

The eai gateway extension: agent, session, run, task and data classification.

This element takes no parameter.

Returns: dict[str, Any]. Dict of the identity fields that are set, without empty values.

AgentRunner.execute​

execute(name: str, arguments: dict[str, Any]) -> Any

Run a local, MCP or Tool Gateway tool.

NameTypeDefaultRequiredDescription
namestryesName of the tool requested by the model.
argumentsdict[str, Any]yesArguments of the tool call, decoded from the model JSON.

Returns: Any. Result of the tool; {error: rejected_by_approver} when a Tool Gateway approval is refused; raises EaiError tool_not_found when nothing runs it.

Raises: EaiError.

AgentRunner.limit_reached​

limit_reached(steps: int, tool_calls: int, cost: Decimal, started: float) -> str | None

Check the step, cost and duration limits of the budget.

NameTypeDefaultRequiredDescription
stepsintyesModel calls made in the turn so far.
tool_callsintyesTool calls made so far; the tool call limit itself is checked before each tool call.
costDecimalyesCost spent in the turn so far, as a Decimal.
startedfloatyestime.monotonic() value at the start of the turn.

Returns: str \| None. max_steps, max_cost or max_duration for the first limit reached, otherwise None.

AgentRunner.needs_approval​

needs_approval(name: str) -> bool

Tell whether a tool call needs a human approval.

NameTypeDefaultRequiredDescription
namestryesName of the tool.

Returns: bool. True when the local tool has requires_approval or the name matches approval_for.

AgentRunner.record​

record(step: dict[str, Any]) -> None

Report a step on the run (POST /v1/runs/{id}/steps); does nothing without a client or a run.

NameTypeDefaultRequiredDescription
stepdict[str, Any]yesStep document, such as {kind: model_call, name, state} or {kind: tool_call, name, state}.

Returns: None.

AgentRunner.run_turn​

run_turn(messages: list[dict[str, Any]], state: dict[str, Any] | None = None) -> TurnResult

Run one turn. state (a checkpoint) resumes a turn that was interrupted.

NameTypeDefaultRequiredDescription
messageslist[dict[str, Any]]yesChat messages that open the turn; ignored when state holds messages.
statedict[str, Any] | NoneNonenoCheckpoint state to resume (messages, step, tool calls, nudges, usage).

Returns: TurnResult. A TurnResult: Outcome of :meth:AgentRunner.run_turn.

AgentRunner.tool_specs​

tool_specs() -> list[dict[str, Any]]

Tools offered to the model: the local tools, then the Tool Gateway tools, then the MCP tools.

This element takes no parameter.

Returns: list[dict[str, Any]]. List of OpenAI function tool specs.