Skip to main content

Overview

Fast Agent provides a flexible agent architecture that enables you to create sophisticated AI agents with minimal configuration. Agents are the core building blocks that interact with LLMs, execute tools, and manage conversation state.

Agent Types

Fast Agent supports multiple agent types to handle different use cases:

Basic Agent

Simple agents with tool calling and conversation management

Smart Agent

Enhanced agents with built-in command support and advanced features

Workflow Agents

Specialized agents for orchestrating complex workflows

Custom Agents

Extensible agents for specialized use cases

Agent Type Enumeration

The framework defines the following agent types:
class AgentType(StrEnum):
    LLM = auto()
    BASIC = auto()
    SMART = auto()
    CUSTOM = auto()
    ORCHESTRATOR = auto()
    PARALLEL = auto()
    EVALUATOR_OPTIMIZER = auto()
    ROUTER = auto()
    CHAIN = auto()
    ITERATIVE_PLANNER = auto()
    MAKER = auto()

Agent Configuration

Every agent is configured using the AgentConfig dataclass:
@dataclass
class AgentConfig:
    name: str
    instruction: str = DEFAULT_AGENT_INSTRUCTION
    description: str | None = None
    tool_input_schema: dict[str, Any] | None = None
    servers: list[str] = field(default_factory=list)
    tools: dict[str, list[str]] = field(default_factory=dict)
    resources: dict[str, list[str]] = field(default_factory=dict)
    prompts: dict[str, list[str]] = field(default_factory=dict)
    skills: SkillConfig = SKILLS_DEFAULT
    model: str | None = None
    use_history: bool = True
    default_request_params: RequestParams | None = None
    human_input: bool = False
    agent_type: AgentType = AgentType.BASIC
    default: bool = False
    tool_only: bool = False
    function_tools: FunctionToolsConfig = None
    shell: bool = False
    cwd: Path | None = None
    mcp_connect: list[MCPConnectTarget] = field(default_factory=list)

Key Configuration Options

The system prompt that defines the agent’s behavior and capabilities. This can include template variables like {{agentSkills}} for dynamic content.
List of MCP server names to attach to this agent. Servers provide tools, resources, and prompts.
Whether the agent maintains conversation history across turns. Set to false for stateless agents.
Enables the agent to request input from users during task execution.
Enables shell command execution capabilities for the agent.

Agent Lifecycle

Fast Agent uses an async context manager pattern for agent lifecycle management:
from fast_agent import FastAgent

fast = FastAgent("My App")

@fast.agent(
    name="assistant",
    instruction="You are a helpful AI assistant."
)
async def main():
    async with fast.run() as agent:
        # Agent is initialized and ready
        response = await agent("Hello!")
        print(response)
    # Agent is automatically cleaned up

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

Lifecycle Stages

  1. Creation: Agent configuration is registered via decorator
  2. Initialization: MCP servers are connected, tools are loaded
  3. Execution: Agent processes messages and executes tools
  4. Shutdown: Connections are closed, resources are cleaned up
The async with fast.run() pattern ensures proper initialization and cleanup of all agents and their MCP server connections.

Creating Agents

Decorator Syntax

The simplest way to create an agent:
@fast.agent(
    name="calculator",
    instruction="Perform mathematical calculations.",
    servers=["math"]
)

Programmatic Creation

For dynamic agent creation:
from fast_agent.agents.agent_types import AgentConfig, AgentType
from fast_agent.agents.mcp_agent import McpAgent

config = AgentConfig(
    name="custom_agent",
    instruction="Custom instructions",
    agent_type=AgentType.BASIC,
    servers=["filesystem"],
    use_history=True
)

agent = McpAgent(config=config, context=context)
await agent.initialize()

Agent Communication

Agents support multiple ways to interact:

Direct Messaging

# Simple string message
response = await agent("What is 2+2?")

# Explicit send method
response = await agent.send("Tell me a joke")

Interactive Sessions

# Start an interactive chat session
await agent.interactive()

# Or with explicit method call
await agent.prompt()

With Resources

# Attach a resource to the message
response = await agent.with_resource(
    "Summarize this document",
    "resource://server/document.pdf"
)

MCP Integration

Agents seamlessly integrate with MCP (Model Context Protocol) servers:

Server Attachment

config = AgentConfig(
    name="file_agent",
    instruction="Help with file operations",
    servers=["filesystem"]  # MCP server name from config
)

Runtime Server Management

# Attach server at runtime
result = await agent.attach_mcp_server(
    server_name="new_server",
    server_config=server_settings
)

# Detach server
result = await agent.detach_mcp_server("server_name")

# List attached servers
servers = agent.list_attached_mcp_servers()
See the MCP Integration page for detailed information about the Model Context Protocol support.

Agent Capabilities

Tool Execution

Agents can execute tools from attached MCP servers:
# Tools are automatically available from MCP servers
result = await agent.call_tool(
    name="filesystem__read_file",
    arguments={"path": "/path/to/file.txt"}
)

Prompt Templates

Apply MCP prompt templates:
# Apply a prompt template from an MCP server
response = await agent.apply_prompt(
    "template_name",
    arguments={"variable": "value"}
)

Resource Access

Access resources from MCP servers:
# Get a resource
resource = await agent.get_resource(
    resource_uri="resource://server/path",
    namespace="server_name"
)

# List available resources
resources = await agent.list_resources(namespace="server_name")

State Management

Conversation History

Agents maintain conversation history based on the use_history setting:
@fast.agent(
    name="stateful_agent",
    instruction="Remember our conversation",
    use_history=True  # Maintains history
)

@fast.agent(
    name="stateless_agent",
    instruction="Process each message independently",
    use_history=False  # No history
)

Clearing History

# Clear conversation history
agent.clear(clear_prompts=True)

# Access message history
history = agent.message_history

Advanced Features

Shell Runtime

Enable shell command execution:
@fast.agent(
    name="dev_agent",
    instruction="Assist with development tasks",
    shell=True,  # Enable shell access
    cwd=Path("/project")  # Set working directory
)

Skills Integration

Agents can load and use skills:
@fast.agent(
    name="skilled_agent",
    instruction="Use available skills. {{agentSkills}}",
    skills=SKILLS_DEFAULT  # Load default skills
)

Human Input

Enable agents to request human input:
@fast.agent(
    name="interactive_agent",
    instruction="Request clarification when needed",
    human_input=True
)

Best Practices

Instruction Design

Write clear, specific instructions that define the agent’s role, capabilities, and constraints. Use template variables for dynamic content.

History Management

Enable history for conversational agents, disable for task-specific agents to reduce token usage.

Server Selection

Only attach MCP servers that the agent needs. Use tool filters to limit which tools are exposed.

Error Handling

Always use the context manager pattern to ensure proper cleanup of resources and connections.

Next Steps

Workflows

Learn about workflow patterns for complex agent orchestration

MCP Integration

Deep dive into Model Context Protocol integration

Configuration

Configure agents and MCP servers

Examples

See practical examples of agent patterns

Build docs developers (and LLMs) love