Agent
TheAgent constructor defines what your agent is — identity, model, tools, and behavior.
Reasoning override precedence
Reasoning values are resolved in this order:- Run context override:
context[\"_afk\"][\"reasoning\"] - Agent defaults:
reasoning_enabled,reasoning_effort,reasoning_max_tokens - Provider defaults/validation in the LLM layer
RunnerConfig
Passed toRunner(config=RunnerConfig(...)) to control runtime behavior.
Deep Dive: Interaction Models
Theinteraction_mode setting fundamentally changes how the Runner handles decision points like tool approval or user input requests.
-
headless(default): The Runner never pauses.- If a policy returns
deferorrequest_user_input, the Runner immediately uses the configuredapproval_fallbackorinput_fallback(default:deny). - Use case: Backend workers, cron jobs, automated testing.
- If a policy returns
-
interactive: The Runner pauses execution and uses the configuredInteractionProviderto ask for human input.- For CLI apps, this prints to stdout and reads from stdin.
- The run blocks until input is received or
approval_timeout_sexpires. - Use case: Local CLI tools, scripts run by humans.
-
external: The Runner emits arun_pausedevent and suspends execution.- The
run()loop exits (or yields a paused state). The state is persisted to memory. - The system waits for an external API call to
runner.resume_with_input(). - Use case: Chatbots, web UIs, Slack bots where the user is asynchronous.
- The
FailSafeConfig
Passed toAgent(fail_safe=FailSafeConfig(...)) to set runtime limits and failure policies.
Deep Dive: Failure & Recovery
FailSafeConfig controls the agent’s resilience. The policies work in a hierarchy:-
Lower-level retries: Transient errors (network glitches, rate limits) are retried automatically by the LLM client, guided by
AFK_LLM_MAX_RETRIES. -
llm_failure_policy: If the LLM call fails after all retries (or hits a terminal error like 401 Unauthorized):retry_then_fail: Tries a few more times at the agent level, then fails the run.retry_then_degrade: Tries again, then marks the run asdegradedbut returns partial results (useful for “best effort” responses).
-
tool_failure_policy: If a tool raises an exception:continue_with_error(default): The error message is fed back to the model. The model can then try to fix its mistake or apologize. This is usually the best setting for capable models.fail_run: Immediately stops the run. Use this for critical transactional agents where any error is unacceptable.
-
Circuit Breakers:
- If a model provider fails
breaker_failure_thresholdtimes in a row, the circuit opens. - Subsequent calls fail instantly without network traffic until
breaker_cooldown_spasses. - This protects your system (and wallet) from hammering a down service.
- If a model provider fails
FailurePolicy values
SandboxProfile
Controls execution restrictions for tool handlers. Configured viaRunnerConfig.default_sandbox_profile.
Runner constructor
TheRunner accepts these arguments directly (outside of RunnerConfig):
@tool decorator
Next steps
Environment Variables
Environment variable defaults and backend selection.
API Reference
Quick import reference.