Agents API Reference¶
Complete API reference for Agent, AgentSettings, Runner, RunResult, and StreamResult.
AgentSettings¶
AgentSettings
dataclass
¶
AgentSettings(
max_turns: int = 10, model_settings: ModelSettings = ModelSettings()
)
Agent-level settings.
@dataclass
class AgentSettings:
"""Agent-level settings."""
max_turns: int = 10
model_settings: ModelSettings = field(default_factory=ModelSettings)
Agent-level settings that control execution behavior. Passed as the settings field on an Agent.
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
max_turns |
int |
10 |
Maximum number of LLM turns before the run is aborted with MaxTurnsExceeded. |
model_settings |
ModelSettings |
ModelSettings() |
Model generation settings (temperature, max_tokens, etc.) applied to every request this agent makes. |
Usage¶
from flux.agent import AgentSettings
from flux.models.base import ModelSettings
# Use defaults
settings = AgentSettings()
# Custom settings
settings = AgentSettings(
max_turns=20,
model_settings=ModelSettings(temperature=0.5, max_tokens=4096),
)
Note
AgentSettings.max_turns is resolved at run time: the Runner takes the minimum of agent.settings.max_turns and config.default_max_turns.
Agent¶
Agent
dataclass
¶
Agent(
name: str,
instructions: str | Callable[..., str] = "",
model: str | Model | None = None,
tools: tuple[Tool, ...] = (),
handoffs: tuple[Handoff | Agent, ...] = (),
guardrails: tuple[InputGuardrail | OutputGuardrail, ...] = (),
output_type: type | None = None,
settings: AgentSettings = AgentSettings(),
)
An agent that can use tools, hand off to other agents, and follow guardrails.
Agent is immutable — use clone() to create modified copies.
Methods:¶
get_instructions
¶
get_instructions(context: RunContext | None = None) -> str
Resolve instructions (handles both string and callable).
@dataclass(frozen=True)
class Agent:
"""An agent that can use tools, hand off to other agents, and follow guardrails.
Agent is immutable — use clone() to create modified copies.
"""
name: str
instructions: str | Callable[..., str] = ""
model: str | Model | None = None
tools: tuple[Tool, ...] = ()
handoffs: tuple[Handoff | Agent, ...] = ()
guardrails: tuple[InputGuardrail | OutputGuardrail, ...] = ()
output_type: type | None = None
settings: AgentSettings = field(default_factory=AgentSettings)
The core abstraction in Flux. An Agent represents an autonomous entity that can:
- Receive instructions (static string or dynamic callable).
- Use tools to interact with external systems.
- Hand off conversations to other agents.
- Enforce input and output guardrails.
- Produce structured output via
output_type.
Because the dataclass is frozen, all fields are immutable after construction. Use clone() to create modified copies.
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
required | A human-readable name for the agent. Used in logs, events, and as the default handoff tool name prefix. |
instructions |
str \| Callable[..., str] |
"" |
The system prompt. If a callable, it is invoked at run time with the current RunContext and must return a string. |
model |
str \| Model \| None |
None |
Model to use. A string is resolved via the ModelRegistry; a Model instance is used directly. When None, the config default is used. |
tools |
tuple[Tool, ...] |
() |
Tools the agent is allowed to call. |
handoffs |
tuple[Handoff \| Agent, ...] |
() |
Other agents this agent can hand off to. An Agent is auto-wrapped in a Handoff with a default tool name of transfer_to_{agent.name}. |
guardrails |
tuple[InputGuardrail \| OutputGuardrail, ...] |
() |
Guardrails evaluated before/after model calls. |
output_type |
type \| None |
None |
If set, the Runner expects structured output matching this type. |
settings |
AgentSettings |
AgentSettings() |
Agent-level execution settings. |
Methods¶
get_instructions¶
Resolve instructions to a string. If instructions is a callable, it is called with the provided RunContext (or None).
| Parameter | Type | Description |
|---|---|---|
context |
RunContext \| None |
Optional run context passed to callable instructions. |
Returns: str — the resolved instruction string.
clone¶
Create a modified copy of this agent. Accepts any field name as a keyword argument.
| Parameter | Type | Description |
|---|---|---|
**kwargs |
Any |
Fields to override in the new agent. |
Returns: Agent — a new immutable Agent instance.
Usage¶
from flux.agent import Agent, AgentSettings
from flux.tools.decorator import tool
from flux.models.base import ModelSettings
# Simple agent with a tool
@tool
def get_weather(city: str) -> str:
"""Get current weather for a city."""
return f"Sunny, 72F in {city}"
assistant = Agent(
name="WeatherBot",
instructions="You are a helpful weather assistant.",
tools=[get_weather],
settings=AgentSettings(max_turns=5),
)
# Dynamic instructions
def dynamic_instructions(ctx):
return f"The user's name is {ctx.user_context}." if ctx else "Hello!"
agent = Agent(
name="DynamicAgent",
instructions=dynamic_instructions,
)
# Clone with overrides
specialist = assistant.clone(
name="WeatherSpecialist",
instructions="You specialize in weather forecasts.",
)
Runner¶
Runner
¶
Execution engine for Flux agents.
Methods:¶
run
async
staticmethod
¶
run(
agent: Agent,
input: str | list[Message],
*,
context: Any = None,
config: FluxConfig | None = None,
session: Any = None,
model: Model | None = None,
) -> RunResult
Run an agent to completion.
| PARAMETER | DESCRIPTION |
|---|---|
agent
|
The agent to run.
TYPE:
|
input
|
User input as string or message list.
TYPE:
|
context
|
Optional user context object.
TYPE:
|
config
|
Optional configuration override.
TYPE:
|
session
|
Optional session for conversation persistence.
TYPE:
|
model
|
Optional model override.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RunResult
|
RunResult with the agent's final output. |
Source code in flux\runner.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
run_sync
staticmethod
¶
Synchronous wrapper for Runner.run().
Source code in flux\runner.py
run_streamed
async
staticmethod
¶
run_streamed(
agent: Agent,
input: str | list[Message],
*,
context: Any = None,
config: FluxConfig | None = None,
model: Model | None = None,
) -> StreamResult
Run an agent with streaming output.
Source code in flux\runner.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
The Runner drives the agent loop: it sends messages to the LLM, executes tools, processes handoffs, and enforces guardrails. All methods are static.
Methods¶
run¶
@staticmethod
async def run(
agent: Agent,
input: str | list[Message],
*,
context: Any = None,
config: FluxConfig | None = None,
session: Any = None,
model: Model | None = None,
) -> RunResult:
Run an agent to completion. This is the primary entry point for executing an agent.
| Parameter | Type | Default | Description |
|---|---|---|---|
agent |
Agent |
required | The agent to run. |
input |
str \| list[Message] |
required | User input as a plain string or a pre-built list of Message objects. |
context |
Any |
None |
Optional user context object, accessible via RunContext.user_context. |
config |
FluxConfig \| None |
None |
Configuration override. Falls back to the global config via get_config(). |
session |
Any |
None |
Optional Session instance for conversation persistence. History is loaded before the run and new messages are saved after. |
model |
Model \| None |
None |
Model override. Takes precedence over the agent's own model. |
Returns: RunResult
Raises:
| Exception | Condition |
|---|---|
MaxTurnsExceeded |
The agent exceeds the configured maximum turns. |
ModelBehaviorError |
The model returns an empty response. |
InputGuardrailTripwireTriggered |
An input guardrail blocks the request. |
OutputGuardrailTripwireTriggered |
An output guardrail blocks the response. |
ProviderError |
The LLM provider returns an error. |
run_sync¶
@staticmethod
def run_sync(
agent: Agent,
input: str | list[Message],
**kwargs: Any,
) -> RunResult:
Synchronous wrapper around Runner.run(). Blocks until the run completes.
Uses asyncio.run() when no event loop is running, or delegates to a thread pool when an event loop is already active.
| Parameter | Type | Description |
|---|---|---|
agent |
Agent |
The agent to run. |
input |
str \| list[Message] |
User input. |
**kwargs |
Any |
All keyword arguments accepted by Runner.run(). |
Returns: RunResult
run_streamed¶
@staticmethod
async def run_streamed(
agent: Agent,
input: str | list[Message],
*,
context: Any = None,
config: FluxConfig | None = None,
model: Model | None = None,
) -> StreamResult:
Run an agent with streaming output. Returns a StreamResult that yields StreamEvent objects as they arrive.
| Parameter | Type | Default | Description |
|---|---|---|---|
agent |
Agent |
required | The agent to run. |
input |
str \| list[Message] |
required | User input. |
context |
Any |
None |
Optional user context. |
config |
FluxConfig \| None |
None |
Configuration override. |
model |
Model \| None |
None |
Model override. |
Returns: StreamResult
Usage¶
import asyncio
from flux.agent import Agent
from flux.runner import Runner
agent = Agent(name="Assistant", instructions="You are helpful.")
# Async
async def main():
result = await Runner.run(agent, "Hello!")
print(result.final_output)
# Sync
result = Runner.run_sync(agent, "Hello!")
print(result.final_output)
# Streaming
async def stream_main():
stream = await Runner.run_streamed(agent, "Tell me a story.")
async for event in stream:
if hasattr(event, "delta"):
print(event.delta, end="")
RunResult¶
RunResult
dataclass
¶
RunResult(
final_output: Any = None,
last_agent: Agent | None = None,
usage: Usage = Usage(),
messages: list[Message] = list(),
handoffs: list[dict[str, Any]] = list(),
turns: int = 0,
)
Result of a completed agent run.
@dataclass
class RunResult:
"""Result of a completed agent run."""
final_output: Any = None
last_agent: Agent | None = None
usage: Usage = field(default_factory=Usage)
messages: list[Message] = field(default_factory=list)
handoffs: list[dict[str, Any]] = field(default_factory=list)
turns: int = 0
Returned by Runner.run() and Runner.run_sync() when a run completes successfully.
Fields¶
| Field | Type | Description |
|---|---|---|
final_output |
Any |
The agent's final output. Typically a string, but can be any type if output_type is set. |
last_agent |
Agent \| None |
The agent that produced the final output (may differ from the original if handoffs occurred). |
usage |
Usage |
Aggregated token usage across all model calls. |
messages |
list[Message] |
Full conversation history for the run. |
handoffs |
list[dict[str, Any]] |
List of handoff events. Each dict contains source, target, and tool_name. |
turns |
int |
Total number of LLM turns executed. |
Usage¶
result = Runner.run_sync(agent, "What is the capital of France?")
print(result.final_output) # "The capital of France is Paris."
print(result.last_agent.name) # "Assistant"
print(result.turns) # 1
print(result.usage.total_tokens) # 42
for msg in result.messages:
print(f"{msg.role}: {msg.content}")
StreamResult¶
StreamResult
¶
StreamResult(agent: Agent, gen: AsyncIterator[StreamEvent])
class StreamResult:
"""Result of a streamed agent run."""
current_agent: Agent
def __init__(self, agent: Agent, gen: AsyncIterator[StreamEvent]) -> None: ...
def __aiter__(self) -> AsyncIterator[StreamEvent]: ...
async def receive(self) -> StreamEvent: ...
Returned by Runner.run_streamed(). Provides an async iterator over StreamEvent objects and tracks the current active agent.
Attributes¶
| Attribute | Type | Description |
|---|---|---|
current_agent |
Agent |
The currently active agent. Updates on handoffs. |
Methods¶
receive¶
Receive the next streaming event. Raises StopAsyncIteration when the stream is exhausted.
Returns: StreamEvent — one of TextDeltaEvent, ToolCallEvent, MessageCompleteEvent, UsageEvent, AgentUpdatedEvent, or ErrorEvent.
Usage¶
import asyncio
from flux.runner import Runner
async def main():
agent = Agent(name="Writer", instructions="You write stories.")
stream = await Runner.run_streamed(agent, "Write a haiku about code.")
# Option 1: async for
async for event in stream:
if hasattr(event, "delta"):
print(event.delta, end="", flush=True)
# Option 2: explicit receive
stream = await Runner.run_streamed(agent, "Write a haiku about code.")
try:
while True:
event = await stream.receive()
if hasattr(event, "delta"):
print(event.delta, end="", flush=True)
except StopAsyncIteration:
pass
asyncio.run(main())
Stream Event Types¶
| Event | Description |
|---|---|
AgentUpdatedEvent |
Emitted when the active agent changes (handoff). |
TextDeltaEvent |
Incremental text token from the model. |
ToolCallEvent |
A complete tool call with id, name, and arguments. |
MessageCompleteEvent |
The full message after streaming finishes. |
UsageEvent |
Token usage update. |
ErrorEvent |
An error during streaming. |