Skip to main content

@agentic

A decorator used to create an agentic function.
def agentic[**I, O](
    *scope_defined: Any,
    scope: dict[str, Any] | None = None,
    system: str | None = None,
    premise: str | None = None,
    mcp: str | None = None,
    persist: bool = False,
    model: ModelStrings = DEFAULT_AGENT_MODEL,
    listener: Callable[[], AgentListener] | DefaultAgentListener | None = DEFAULT_AGENT_LISTENER,
    max_tokens: int | MaxTokens = MaxTokens.default(),
    _logging: bool = False,
) -> Callable[[Callable[I, O]], Callable[I, O]]:
    ...
Parameters
*scope_defined
Any
A list of runtime resources as in scope. The names of the resources are not specified explicitly and are instead derived automatically from the resources themselves. scope and scope_defined can be used together to specify resources with both explicit and implicit names. The names can’t be repeated between the two. Example:
@agentic(my_func, db_connection, cache) # the same as @agentic(scope={"my_func": my_func, "db_connection": db_connection, "cache": cache})
async def my_function():
    ...
scope
dict[str, Any] | None = None
A dictionary of names mapped to runtime resources that are in scope and which may be used during the execution of the agentic function. Resources in scope may be arbitrary Python functions, methods, objects, iterators, types or any other Python value.
system
str | None
An optional system prompt for the agentic function. This will be the system prompt of all invocations of this agentic function.
This argument cannot be provided along with the premise argument.
premise
str | None
An optional premise for the agentic function. This will be attached to the system prompt of all invocations of this agentic function.
This argument cannot be provided along with the system argument.
mcp
str | None
The string of a path to a .json file representing an MCP configuration. Any servers and/or tools of servers outlined in the config can be used during the execution of the agentic function.
command
string
required
The executable command to run the MCP server. This should be an absolute path or a command available in the system PATH.Example:
"python"
args
array of string
An array of command-line arguments passed to the server executable. Arguments are passed in order.Example:
["server.py", "--verbose", "--port", "8080"]
env
object
An object containing environment variables to set when launching the server. All values must be strings.Example:
{
    "API_KEY": "secret-key",
    "PORT": "8080"
}
persist
bool
Whether to persist the function state/history between calls. Defaults to False.
model
str
The model which backs your agent.One of:
  • 'openai:gpt-3.5-turbo'
  • 'openai:gpt-4o'
  • 'openai:gpt-4.1'
  • 'openai:gpt-5'
  • 'anthropic:claude-sonnet-4'
  • 'anthropic:claude-opus-4.1'
  • 'anthropic:claude-sonnet-4.5'
  • 'anthropic:claude-opus-4.5'
or any OpenRouter model slug.
The default model is openai:gpt-4.1.
max_tokens
int | MaxTokens
Either
  • the maximum number of output tokens generated in a single round of inference in a single invocation, or
  • an instance of MaxTokens for more fine-grained control.
See MaxTokens for information on default values.
listener
Callable[[], AgentListener] | DefaultAgentListener | None
Optional listener constructor for logging the agentic function’s activity and chat history.
The default agent listener is the StandardListener, but can be changed for all agents and agentic functions in the current scope with set_default_agent_listener. If a context-specific logger is used in the current scope, the logger will be added to the listener: if the listener is None, then the listener will be set to:
  • the default agent listener, if it is not None, or
  • the StandardListener, if the default agent listener is None
For more information on the StandardListener and the listener heirarchy, see here.
Returns
Callable[[Callable[I, O]], Callable[I, O]]
The decorated agentic function.