Skip to main content

Agents

Agents are the core building block of BindAI. An agent combines a language model provider with instructions and optional capabilities such as tools, memory, knowledge, retrieval, middleware, hooks, callbacks, execution configuration, delegation, and multi-agent coordination. Agents provide a consistent interface for executing AI tasks without requiring application code to manage provider-specific execution details.

What Is an Agent?

A BindAI agent is responsible for:
  • Receiving user input
  • Executing an AI model
  • Applying configured instructions
  • Calling registered tools
  • Using configured memory
  • Using optional knowledge or retrieval
  • Managing conversation context
  • Applying execution configuration
  • Running middleware and lifecycle hooks
  • Returning an AgentResult
The Agent class provides the primary execution interface.

Creating an Agent

The recommended way to create an agent is with Agent.builder().
The builder provides a fluent interface for configuring the agent before construction. A basic agent can then be executed with:

Agent Builder

Agent.builder() returns an AgentBuilder that can configure the provider, model, instructions, tools, memory, knowledge, retrieval, middleware, hooks, execution configuration, and events. Common builder methods include:
The builder supports configuration for:
  • Name
  • Model
  • Provider
  • Instructions
  • Temperature
  • Maximum output tokens
  • Maximum tool iterations
  • Tools
  • Memory
  • Knowledge
  • Retrievers
  • Middleware
  • Hooks
  • Events
The fluent API makes it possible to start with a simple agent and add capabilities as an application grows.

Model Configuration

Models are specified using a provider-qualified model name:
For example:
The model() method separates the provider name from the model name and resolves the provider through BindAI’s provider system. Current provider integrations include:
  • OpenAI
  • Anthropic
  • Google Gemini
  • Groq
  • Ollama
  • OpenRouter
For example:
Exact model availability depends on the provider and model configured for the application. The builder also provides a dedicated OpenAI helper:

Provider Configuration

Providers can be configured explicitly with provider():
Provider configuration can also include provider-specific settings:
An already-created ModelProvider can also be supplied:
This allows applications to either use BindAI’s registered provider configuration or supply an existing provider implementation.

Project-Based Provider Configuration

An agent can use project configuration instead of specifying provider settings directly in application code. For example, a project can define:
When the builder has not been given an explicit provider, build() loads the project runtime configuration and uses the configured provider, model, connection, and temperature. The builder can also explicitly load project configuration with from_project():
from_project() loads project-defined tools and provider configuration. This allows an agent definition to remain focused on application behavior while project-level configuration controls provider selection and other runtime settings.

Project-Scoped Provider Connections

A project can select a named provider connection instead of placing provider credentials directly in application code or bindai.toml. For example:
The connection metadata identifies the provider while the credential is stored separately in the operating system credential store. The same connection can be selected directly through the builder:
Provider credentials are resolved with the following precedence:
  1. An explicitly supplied API key.
  2. The credential stored for the selected project connection.
  3. The provider’s environment variable.
The configured connection must exist and must match the selected provider. Project-scoped provider connections are documented in the Connections section.

Agent Configuration

Execution behavior can be configured independently from the agent’s identity and capabilities. For example:
The available builder configuration includes:
  • temperature() - configures model sampling behavior.
  • max_tokens() - configures the maximum output token count.
  • max_tool_iterations() - limits tool execution iterations.
The configuration is stored on the built agent and is used by the execution system. Keeping execution configuration separate from the agent’s identity and capabilities allows the same agent definition to be used with different runtime settings.

Instructions

Instructions define the system-level behavior of an agent. For example:
Instructions can also be accessed through the agent:
The instructions are stored as part of the agent’s prompt configuration.

Running an Agent

Use the run() method as the primary application-level execution entry point.
run() creates the execution context and delegates to the agent’s conversational execution path. The method accepts an optional output type:

Chat Execution

BindAI also provides chat() for conversational execution.
run() and chat() currently use the same execution path. run() is the primary application-facing entry point, while chat() makes the conversational nature of the operation explicit. Structured output can also be requested:
Conversation state is maintained by the agent’s conversation component and can be combined with configured memory.

Lower-Level Execution

When an application already has an ExecutionContext, it can execute the agent directly:
This lower-level interface is useful for framework integrations and components that already manage execution context. The execution context can carry state and variables through the agent execution pipeline.

AgentResult

Agent execution returns an AgentResult.
The result provides execution status, generated output, and error information when execution fails. Applications can use the result to determine whether an execution completed successfully before consuming the generated output.

Sending Prompts

The primary execution methods accept a message:
The message is placed into the execution context and processed by the agent execution engine. Applications can request structured output by supplying a Python type:

Structured Output

BindAI supports structured output using a Python type. For example, a Pydantic model can define the expected result:
Pass the model to run():
The output type is placed into the execution context and made available to the execution pipeline. Structured output is useful when application code needs predictable data rather than unrestricted text.

Streaming

Agents support streaming execution through two interfaces. For normal message-based streaming, use stream_chat():
When an application already has an ExecutionContext, use stream():
stream_chat() creates the execution context from the supplied message, while stream() operates on an existing execution context. Both methods delegate streaming to the agent execution engine. Streaming is useful for interactive applications that need to process model output progressively rather than waiting for the complete execution.

Adding Tools

Agents can execute registered tools. A tool can be attached directly to an agent:
For example:
Tools can also be added after construction:
Multiple tools can be registered:
The builder provides tools() for registering multiple tools during construction:
The builder accepts existing Tool objects as well as callable functions that can be converted into BindAI function tools. add_tool() is available as a backwards-compatible alias for tool().

Tool Calling

When a configured model determines that a tool is required, BindAI can execute the registered tool and incorporate its result into the agent execution. A typical architecture is:
Tools are registered in the agent’s ToolRegistry. Applications can also execute a registered tool directly:
Tool execution can publish a ToolExecutedEvent through the agent event bus.

Memory

Agents have a memory component that is initialized with an in-memory provider by default. A different memory implementation can be attached to an agent:
Memory can also be configured through the builder:
Memory implementations can provide different persistence and retrieval strategies. BindAI supports memory implementations for use cases such as:
  • In-memory state
  • SQLite-backed memory
  • PostgreSQL-backed memory
  • Vector-based memory
  • Pinecone-backed memory
  • Chroma-backed memory
  • Conversation-oriented memory
  • Custom memory implementations
Memory is separate from the immediate execution input and can be replaced independently from the model provider.

Conversation Context

Agents maintain a Conversation component for conversational state. Conversation context allows an application to work with multiple related messages rather than treating every execution as an isolated request. This is useful for:
  • Chat applications
  • Assistants
  • Multi-turn workflows
  • Stateful agent execution
  • Applications combining conversation history with longer-term memory
Conversation state and persistent memory solve related but different problems. Conversation state represents the current conversational interaction, while memory can provide information that persists independently of an individual conversation.

Agent Sessions

Agents provide session() for creating an AgentSession.
Sessions provide a higher-level object for applications that need to work with an agent across a sequence of interactions. Use the session abstraction when an application needs session-oriented agent behavior instead of directly managing every execution against the Agent object.

Knowledge

Agents can be connected to knowledge sources.
Knowledge can also be configured through the builder:
Knowledge integration allows applications to provide external information to agent execution. BindAI’s knowledge system supports concepts such as:
  • Document ingestion
  • Parsing
  • Chunking
  • Embeddings
  • Metadata
  • Retrieval
  • Reranking
  • Conversational retrieval
  • Knowledge pipelines

Retrievers

A retriever can be attached directly to an agent:
Or configured through the builder:
Retrievers are useful when an application needs to search a knowledge source and provide relevant information to an agent. BindAI supports retrieval strategies such as:
  • Vector retrieval
  • BM25 retrieval
  • Hybrid retrieval
Retrieval can therefore combine semantic similarity with lexical matching depending on the application’s requirements.

Retrieval-Augmented Generation

An agent can use retrieved knowledge as additional context during execution. A typical RAG flow is:
This allows the model to answer using information retrieved from an application’s knowledge sources. RAG-enabled agents can also participate in larger workflows and multi-agent systems.

Middleware

Middleware can be attached to an agent using:
The builder provides the corresponding configuration method:
Multiple middleware components can be registered. Middleware provides reusable behavior around agent execution. Typical applications include:
  • Logging
  • Authentication
  • Telemetry
  • Metrics
  • Request processing
  • Response processing
  • Cross-cutting application behavior
Middleware is useful when behavior should be shared across agents rather than implemented separately in every agent.

Hooks and Callbacks

Agents expose lifecycle and callback mechanisms that allow applications to react to execution events. Agents provide callback registration through on():
The built-in lifecycle callback events include:
  • before_run
  • after_run
  • error
For example:
Agents also support hooks through:
Hooks are stored separately from lifecycle callbacks and can be used by application or framework components to participate in agent behavior. These mechanisms can be used for:
  • Logging
  • Auditing
  • Monitoring
  • Analytics
  • Notifications
  • Debugging
  • Custom application behavior

Events

Agents expose an event bus through agent.events. The event bus can be supplied through the builder:
The event bus is separate from the agent’s callback mechanism. The agent callback API uses:
The event bus uses event subscriptions and published event objects. Tool execution can publish events such as ToolExecutedEvent. The event system is useful for:
  • Observability
  • Logging
  • Metrics
  • Application events
  • Debugging
  • Notifications
  • Automation integrations

Execution Configuration

Agent execution can be configured independently from the agent’s core identity and capabilities. The current agent configuration includes settings such as:
  • Temperature
  • Maximum output tokens
  • Maximum tool iterations
For example:
Keeping execution configuration separate from the agent’s core definition allows applications to reuse the same agent configuration in different execution environments.

Execution Context

BindAI uses ExecutionContext to carry information associated with an agent execution. Execution context can provide runtime information required by components such as:
  • Agents
  • Tools
  • Middleware
  • Streaming
  • Workflows
An agent creates an execution context for normal run() and chat() calls. Agents can also execute directly from an existing context:
This lower-level interface is useful for framework integrations and components that already manage an execution context. Structured output configuration is also placed into the execution context before execution.

Agent Delegation

Agents can delegate work to other agents through delegate_to().
Delegation registers the target agent as a tool on the delegating agent. An optional name and description can be supplied:
A typical delegation pattern is:
Each specialist can focus on a narrower responsibility while the primary agent coordinates the overall task.

Agent Teams

BindAI also provides AgentTeam for coordinating a collection of existing agents. A team can register agents directly:
Agents can then be accessed by name or role:
The team provides several execution patterns. Sequential team execution:
Chained execution passes each agent’s output to the next agent:
Parallel execution runs the registered agents concurrently:
A team can also continue running after individual failures:
Role-based execution is available through methods such as:
and:
AgentTeam is a lightweight coordination abstraction around existing agents. It is distinct from the task-oriented Agent Group system provided by bindai-group.

Agent Groups

BindAI also provides an Agent Group abstraction for coordinating multiple agents around explicit tasks. Agent Groups are useful when the application needs task-oriented multi-agent execution rather than simply running a collection of agents. The public group API is provided by bindai-group. A basic group can be created with GroupBuilder:
The main group components are:
  • Group - represents the agent group and its tasks.
  • GroupBuilder - provides the fluent construction API.
  • Task - describes work assigned to a specific agent.
  • GroupResult - contains the overall group execution result.
  • SequentialProcess - executes tasks sequentially.
  • ParallelProcess - executes independent tasks concurrently.
Agent Groups and AgentTeam solve different coordination problems:
  • AgentTeam coordinates an existing collection of agents.
  • Agent Groups represent explicit tasks assigned to agents.
  • SequentialProcess and ParallelProcess control group task execution.
  • Task context can connect dependent tasks.

Sequential Agent Groups

The default group process is sequential execution.
Sequential groups execute tasks in their configured order. Task dependencies can provide previous task results as context:
The dependent task can then use the previous task’s result as context during execution.

Parallel Agent Groups

Independent Agent Group tasks can be executed concurrently using ParallelProcess.
Independent tasks can run at the same time when their work can safely be performed concurrently. The parallel process also respects task dependencies. For example:
Here:
Analysis depends on Research and therefore cannot begin until its dependency has completed. If multiple tasks have no unresolved dependencies, they can execute concurrently:
A later task can then depend on multiple completed tasks:
Task dependencies therefore allow parallel processes to combine concurrent work with explicit sequential dependencies.

Handoffs

BindAI can also be used to implement handoff patterns between specialized agents. A handoff allows responsibility for a task to move from one agent to another when another specialist should continue the execution. A typical pattern is:
Handoffs are useful for applications where different agents have clearly separated responsibilities. For explicit multi-agent execution with task dependencies and process-based orchestration, use AgentTeam or Agent Groups depending on the required coordination model.

Specialist Agents

Specialist agents can be created around specific roles. Examples include:
  • Research agents
  • Writing agents
  • Coding agents
  • Analysis agents
  • Support agents
  • Retrieval-focused agents
A specialist should generally have focused instructions, appropriate tools, and only the knowledge or memory required for its responsibility. This keeps multi-agent systems modular and easier to maintain.

Team Delegation

Multiple agents can participate in team-oriented execution. A team can divide a larger task between agents with different responsibilities. For example:
Different BindAI coordination mechanisms can represent this structure. Dynamic delegation through delegate_to() lets one agent invoke another agent as a tool. AgentTeam provides direct coordination of a collection of existing agents. Agent Groups provide explicit task-oriented orchestration. These mechanisms can therefore be selected according to the application’s coordination requirements.

Specialist Role Chains

Specialists can also be organized into role-oriented execution chains. For example:
This can be implemented with AgentTeam.run_chain() or sequential Agent Group tasks. Each stage can receive the output required from the previous stage. This pattern is useful when an application needs predictable specialization rather than entirely dynamic delegation.

RAG-Enabled Multi-Agent Systems

Knowledge and retrieval capabilities can be combined with delegation and specialist agents. For example:
This allows different agents to use different knowledge sources or retrieval strategies while participating in the same larger application. Agent Teams, Agent Groups, and workflows can be used to make these stages explicit depending on the required orchestration model.

Agents in Workflows

Agents can be used as components within larger BindAI workflows. A workflow can coordinate agents with operations such as:
  • Conditions
  • Loops
  • Parallel execution
  • Retries
  • Timeouts
  • Human tasks
  • Scheduling
This makes it possible to keep individual agents focused on specific responsibilities while using workflows for higher-level orchestration. For example:
Workflows are therefore useful when execution involves multiple explicit stages or control-flow decisions.

Agents in Projects

Agents can be organized as part of a BindAI project. A project can provide the surrounding application structure for:
  • Agents
  • Tools
  • Workflows
  • Knowledge
  • Memory
  • Templates
  • Tests
  • Configuration
This allows agent definitions to remain focused on agent behavior while the project organizes the broader application. Project configuration can also provide provider, model, connection, and temperature settings to AgentBuilder.

Agents and External Integrations

Agents can work with external services through BindAI connections and tools. Connections provide application-level access to external services, while tools can expose that functionality to an agent when appropriate. This makes it possible to build agents that interact with external systems without embedding provider-specific integration logic directly into the agent. Project-scoped provider connections are a separate concept from external service connections. Provider connections select credentials for AI model providers, while external service connections represent integrations with application services.

Agents and MCP

BindAI also supports MCP connections and MCP-discovered tools. MCP integrations can provide agents with access to tools exposed by MCP-compatible services. The MCP integration can provide capabilities such as:
  • MCP client connections
  • Tool discovery
  • Tool calling
  • MCP tools exposed as BindAI tools
  • Connection handling
This provides another way to extend an agent’s capabilities beyond locally defined tools.

Building a Complete Agent

A more complete agent can combine several capabilities:
The same agent can then be executed through the standard interface:
The important principle is that capabilities remain composable. An application can add tools, memory, knowledge, retrieval, middleware, callbacks, and multi-agent behavior without replacing the fundamental agent execution interface.

Agent Architecture

A typical BindAI agent can be viewed as several layers:
The agent also owns the main runtime components used during execution:
For multi-agent applications, the agent can participate in higher-level coordination:
This separation keeps agent behavior modular and allows different applications to compose the capabilities they need.

Best Practices

  • Give each agent a clear responsibility.
  • Keep instructions focused.
  • Prefer reusable tools over increasingly complex prompts.
  • Use memory for information that should persist beyond a single execution.
  • Use knowledge and retrievers for external information.
  • Use middleware for reusable cross-cutting behavior.
  • Use hooks and callbacks for lifecycle behavior.
  • Use the event system for event-driven application integration and observability.
  • Use execution configuration for runtime behavior rather than embedding runtime concerns in prompts.
  • Use specialist agents when responsibilities are clearly separated.
  • Use delegate_to() when an agent should dynamically invoke another specialist.
  • Use AgentTeam when a collection of existing agents should be coordinated directly.
  • Use Agent Groups when multi-agent work should be represented as explicit tasks.
  • Use SequentialProcess when group task order or dependencies require sequential execution.
  • Use ParallelProcess when independent group tasks can safely execute concurrently.
  • Use task context when one group task depends on the result of another.
  • Use workflows when multiple agents or execution stages need explicit orchestration and control flow.
  • Keep external integrations behind connections or tools.
  • Use MCP when an external MCP service provides capabilities that should be exposed to the agent.
  • Use project configuration when provider and model settings should be managed outside application code.
  • Use project-scoped provider connections when credentials should remain in the operating system credential store.
The result is a modular architecture where individual agents remain focused while larger AI applications can be composed from reusable agents, tools, knowledge sources, workflows, connections, and integrations.