Execution
Execution is the process BindAI uses to run an agent against an input and produce a result. An execution can involve the configured model, instructions, tools, memory, knowledge, retrieval, middleware, conversation context, execution configuration, and event handling depending on how the agent is configured. The main application-facing execution interface is the agent’srun() method.
Running an Agent
A basic execution starts withagent.run():
output argument specifies a Python type for structured output.
Execution Result
agent.run() returns an AgentResult.
Structured Output
Execution can return structured results instead of plain text. For example, a Pydantic model can define the expected structure:run():
Tool Execution
Agents can be configured with tools. For example:Advanced Tool Execution
BindAI’s tool execution pipeline supports multiple tool calls within an execution and feeds their results back into the conversation used for the next model request. When a model requests tools, BindAI:- Records the assistant’s tool-call message.
- Executes the requested tools.
- Records each tool result.
- Builds the next model request from the updated conversation.
- Continues the execution pipeline.
- Produces the final
AgentResultwhen execution completes.
Multiple Tool Calls
A model response can request multiple tools during the same execution step. For example:Tool Execution Failures
Tool execution can fail. A failed tool execution is preserved as an explicit tool result rather than being silently represented as a successful value. For example, if a tool returns an execution error:- Retry the operation through another tool call.
- Adjust the request.
- Explain the failure to the user.
- Continue using information already available.
- Stop execution when the failure prevents useful progress.
Tool Iterations
Tool-using agents may perform multiple model/tool interaction cycles during a single execution. BindAI provides execution configuration for controlling runtime behavior such as tool execution limits. For example, when an agent is configured with a maximum tool-iteration setting, the application can prevent an execution from continuing indefinitely through repeated tool interactions. The appropriate limit depends on the application’s requirements and the complexity of the tools being used. Tool iteration limits are especially useful for agents that:- Use multiple tools
- Perform multi-step tool calls
- Depend on external services
- Need predictable execution boundaries
Memory
Memory can be attached to an agent:Conversation Context
Agent execution can also incorporate conversation context. Conversation support is useful when an application needs to preserve the relationship between multiple messages. A conversational execution can conceptually combine:Knowledge
Knowledge can also be attached to an agent:Retrievers
Agents can use a retriever directly:- Vector retrieval
- BM25 retrieval
- Hybrid retrieval
Retrieval-Augmented Generation
Knowledge and retrieval can be combined with agent execution to implement Retrieval-Augmented Generation. A typical execution flow is:Middleware
Middleware provides reusable execution behavior. Attach middleware directly:- Logging
- Authentication
- Telemetry
- Metrics
- Request processing
- Response processing
- Other reusable execution behavior
Execution Context
Some BindAI execution APIs operate with anExecutionContext.
For example, the low-level stream() API accepts an execution context:
run(), chat(), and stream_chat() create the execution context internally.
Streaming
BindAI exposes streaming APIs for execution. The low-levelstream() method accepts an ExecutionContext:
stream_chat():
stream_chat() creates the execution context from the supplied message, while stream() operates on an existing execution context.
Streaming is useful when an application needs to process output progressively.
Common use cases include:
- Chat interfaces
- Interactive applications
- Long-running responses
- Real-time user interfaces
run() execution because the application can consume output as it becomes available.
Chat Execution
BindAI also provides achat() method:
run(), chat() can accept an optional structured-output type:
run() currently delegates to the same conversational execution path used by chat().
Use the execution interface that best matches the application’s interaction model.
For conversational applications, chat() can be combined with BindAI’s conversation and memory capabilities.
Events
Agents expose an event bus throughagent.events.
For example, an application can subscribe to tool execution events:
ToolExecutedEvent after a tool execution completes.
The event bus is distinct from the agent’s lifecycle callback API.
The agent callback API uses:
- Monitoring
- Logging
- Analytics
- Auditing
- Observability
- Notifications
- Automation integrations
Hooks and Callbacks
Agents expose lifecycle and callback mechanisms for execution-related behavior. The built-in lifecycle callbacks are:before_runafter_runerror
- Custom logging
- Notifications
- Auditing
- Metrics
- External integrations
- Debugging
Execution Configuration
BindAI supports execution configuration for controlling runtime behavior. Execution configuration can be used for concerns such as:- Tool execution limits
- Execution boundaries
- Runtime options
- Other agent execution settings
Execution and Projects
Agents can be organized as part of a BindAI project. Project configuration can provide the surrounding application structure for:- Agents
- Tools
- Workflows
- Knowledge
- Memory
- Templates
- Tests
- Application configuration
Agent.builder() rather than relying on an undocumented project-loading constructor.
For example:
Execution and Workflows
Agents can participate in larger workflows. A workflow can coordinate agent execution with operations such as:- Conditions
- Loops
- Parallel execution
- Retries
- Timeouts
- Human tasks
- Scheduling
Execution and Multi-Agent Systems
Agent execution can also be combined with delegation and multi-agent patterns. A primary agent can delegate work to specialist agents:Execution and External Tools
Agent execution can interact with external systems through tools and connections. Current BindAI connection integrations include:- Webhooks
- GitHub
- Slack
- Notion
- Jira
- Discord
- Resend
- Vercel
- Netlify
Execution and MCP
BindAI also supports MCP connections and MCP-discovered tools. MCP tools can become part of an agent’s available execution capabilities. The current MCP integration supports:- MCP client connections
- Tool discovery
- Tool calling
- MCP tools exposed as BindAI tools
- Basic connection handling
Error Handling
Always check the returnedAgentResult when application behavior depends on successful execution.
Execution Boundaries
Agent execution can involve multiple components:- Instructions define behavior.
- Conversation provides interaction context.
- Memory provides persisted information.
- Knowledge and retrieval provide external information.
- Tools provide actions.
- Middleware provides reusable execution behavior.
- The model provider performs model inference.
AgentResultprovides the application-facing result.
A Complete Execution Example
The following example combines basic agent execution with a tool:search tool. BindAI executes the tool, records its result, and continues the model execution with the updated conversation.
The same agent can later be extended with additional capabilities:
Execution Best Practices
- Use
run()for general agent execution. - Use
chat()for conversational execution. - Use streaming when output needs to be processed progressively.
- Use structured output when downstream code requires predictable data.
- Configure reasonable execution and tool limits for tool-using agents.
- Treat tool failures as execution information rather than assuming every tool call succeeds.
- Use memory when persisted information is required.
- Use knowledge and retrievers for external information.
- Use middleware for reusable cross-cutting behavior.
- Use hooks and callbacks for lifecycle integrations.
- Use the event bus for event-driven integrations and observability.
- Check
AgentResult.successwhen execution failure needs to be handled explicitly. - Keep runtime configuration separate from behavioral instructions.
- Use workflows when multiple agents or execution stages need to be coordinated.
- Use Agent Groups when multi-agent work should be represented as explicit tasks.
- Use parallel group execution only when tasks can safely run concurrently.
- Use task dependencies when one task requires the result of another.
- Keep external integrations behind connections or tools.
- Use MCP when an external MCP service provides capabilities that should be exposed to an agent.
