Skip to main content

Overview

The FastAgent class is the core entry point for building Fast Agent applications. It provides decorators for defining agents, manages agent lifecycle, supports AgentCard loading, and handles configuration.

Constructor

FastAgent(
    name: str,
    config_path: str | None = None,
    ignore_unknown_args: bool = False,
    parse_cli_args: bool = True,
    quiet: bool = False,
    environment_dir: str | pathlib.Path | None = None,
    skills_directory: str | pathlib.Path | Sequence[str | pathlib.Path] | None = None,
    **kwargs
)
name
str
required
Name of the application
config_path
str | None
default:"None"
Optional path to YAML configuration file
ignore_unknown_args
bool
default:"False"
Whether to ignore unknown command line arguments when parse_cli_args is True
parse_cli_args
bool
default:"True"
If True, parse command line arguments using argparse. Set to False when embedding FastAgent in another framework (like FastAPI/Uvicorn) that handles its own arguments
quiet
bool
default:"False"
If True, disable progress display, tool and message logging for cleaner output
environment_dir
str | pathlib.Path | None
default:"None"
Override the base fast-agent environment directory (.fast-agent by default)
skills_directory
str | pathlib.Path | Sequence[str | pathlib.Path] | None
default:"None"
Path(s) to skills directory to use instead of default skills directories

Example

from fast_agent import FastAgent

# Basic initialization
fast = FastAgent("my-app")

# With configuration file
fast = FastAgent(
    "my-app",
    config_path="config.yaml",
    quiet=True
)

# Custom environment and skills directories
fast = FastAgent(
    "my-app",
    environment_dir="./my-env",
    skills_directory=["./skills", "./custom-skills"]
)

Properties

context

@property
def context(self) -> Context
Access the application context containing configuration, server registry, and runtime state.
fast = FastAgent("my-app")
config = fast.context.config
servers = fast.context.server_registry

Methods

run()

async def run(self) -> AsyncIterator[AgentApp]
Context manager for running the application. Initializes all registered agents and manages their lifecycle.
This is an async context manager. Use it with async with to ensure proper initialization and cleanup.
fast = FastAgent("my-app")

@fast.agent(name="assistant")
async def assistant():
    pass

async with fast.run() as app:
    response = await app.assistant.send("Hello!")
    print(response.text)

load_agents()

def load_agents(self, path: str | Path) -> list[str]
Load AgentCards from a file or directory and register them as agents. Loading is idempotent - any previously loaded agents from the same path that are no longer present will be removed.
path
str | Path
required
Path to AgentCard file (.md, .yaml) or directory containing AgentCard files
Returns: Sorted list of agent names loaded from the provided path
fast = FastAgent("my-app")

# Load from directory
names = fast.load_agents("./agent-cards")
print(f"Loaded agents: {names}")

# Load single AgentCard file
fast.load_agents("./agents/specialist.md")

load_agents_from_url()

def load_agents_from_url(self, url: str) -> list[str]
Load an AgentCard from a URL (Markdown or YAML format).
url
str
required
URL to AgentCard file (must end in .md, .markdown, .yaml, or .yml)
Returns: List of agent names loaded from the URL
fast = FastAgent("my-app")

# Load from GitHub
names = fast.load_agents_from_url(
    "https://raw.githubusercontent.com/example/agents/main/specialist.md"
)

attach_agent_tools()

def attach_agent_tools(
    self,
    parent_name: str,
    child_names: Sequence[str]
) -> list[str]
Attach loaded agents to a parent agent via Agents-as-Tools, allowing the parent to invoke child agents as tools.
parent_name
str
required
Name of the parent agent
child_names
Sequence[str]
required
Names of child agents to attach as tools
Returns: List of newly attached agent names (excludes duplicates)
fast = FastAgent("my-app")

# Load some agents
fast.load_agents("./agents")

# Attach specialists to coordinator
added = fast.attach_agent_tools(
    parent_name="coordinator",
    child_names=["code_expert", "docs_expert"]
)
print(f"Attached: {added}")

detach_agent_tools()

def detach_agent_tools(
    self,
    parent_name: str,
    child_names: Sequence[str]
) -> list[str]
Detach agents-as-tools from a parent agent.
parent_name
str
required
Name of the parent agent
child_names
Sequence[str]
required
Names of child agents to detach
Returns: List of successfully detached agent names
removed = fast.detach_agent_tools(
    parent_name="coordinator",
    child_names=["code_expert"]
)

reload_agents()

async def reload_agents(self) -> bool
Reload all previously registered AgentCard roots. Detects changes to AgentCard files, tool files, and history files. Returns: True if any changes were detected and applied, False otherwise
fast = FastAgent("my-app")
fast.load_agents("./agents")

# Later, reload to pick up changes
changed = await fast.reload_agents()
if changed:
    print("Agents reloaded successfully")

get_default_agent_name()

def get_default_agent_name(self) -> str | None
Find the default agent name from the registration data. Returns the name of the first agent with config.default=True, excluding tool_only agents. Falls back to the first non-tool_only agent if no explicit default is set. Returns: The name of the default agent, or None if no agents are registered
fast = FastAgent("my-app")

@fast.agent(name="main", default=True)
async def main():
    pass

default = fast.get_default_agent_name()
print(f"Default agent: {default}")  # "main"

dump_agent_card_text()

def dump_agent_card_text(
    self,
    name: str,
    *,
    as_yaml: bool = False
) -> str
Render an AgentCard as text (Markdown or YAML).
name
str
required
Name of the agent to export
as_yaml
bool
default:"False"
If True, export as YAML; otherwise export as Markdown
Returns: AgentCard content as a string
fast = FastAgent("my-app")

# Export as Markdown
markdown = fast.dump_agent_card_text("assistant")
print(markdown)

# Export as YAML
yaml_content = fast.dump_agent_card_text("assistant", as_yaml=True)

Command Line Arguments

When parse_cli_args=True (default), FastAgent supports the following command line arguments:
--model
str
Override the default model for all agents
--agent
str
Agent name for —message/—prompt-file (defaults to the app default agent)
-m, --message
str
Message to send to the specified agent
-p, --prompt-file
str
Path to a prompt file to use (either text or JSON)
--quiet
bool
Disable progress display, tool and message logging for cleaner output
--version
bool
Show version and exit
--transport
str
Transport protocol to use when running as a server (sse, http, stdio, or acp)
--port
int
default:"8000"
Port to use when running as a server with SSE transport
--host
str
default:"0.0.0.0"
Host address to bind to when running as a server with SSE transport
--instance-scope
str
default:"shared"
Control MCP agent instancing behaviour (shared, connection, request)
--env
str
Override the base fast-agent environment directory
--skills
str
Path to skills directory to use instead of default skills directories
--reload
bool
Enable manual AgentCard reloads (/reload command)
--watch
bool
Watch AgentCard paths and reload when files change
--card-tool
str
Path or URL to an AgentCard file to load as a tool (repeatable)

Example Usage

# Run with custom model
python app.py --model openai:gpt-4

# Send a message
python app.py --message "Hello, agent!"

# Run as HTTP server
python app.py --transport http --port 8080

# Enable auto-reload on AgentCard changes
python app.py --watch

# Load external AgentCard as tool
python app.py --card-tool https://example.com/agent.md

See Also

Build docs developers (and LLMs) love