AI Agents
The complete guide to AI Agents in BoxLang, covering creation, memory management, tool usage, configuration, and advanced patterns.
AI Agents are autonomous entities that can reason, use tools, and maintain conversation memory. Inspired by LangChain agents but "Boxified" for simplicity and productivity, agents handle complex AI workflows by automatically managing state, context, and tool execution.
π Table of Contents
π― What are AI Agents?
An agent is more than a simple chat interface - it's an intelligent entity that:
Maintains Memory: Remembers conversation history across interactions
Uses Tools: Can call functions to access data, perform calculations, or interact with systems
Reasons and Plans: Determines when and how to use tools to accomplish tasks
Manages State: Automatically handles message history and context
Integrates with Pipelines: Works seamlessly in BoxLang AI pipelines
Delegates to Sub-Agents: Can orchestrate specialized sub-agents for complex tasks
ποΈ Agent Architecture
π Agent Decision Flow
π Creating Agents
Basic Agent
Agent with Custom Model
Agent with Tools
Tools enable agents to perform real-world actions:
π Memory Management
Agents automatically maintain conversation history:
π Memory Flow
Window Memory (Default)
Multiple Memory Systems
Agents can use multiple memory instances:
βοΈ Configuration
Constructor-Based Configuration
Agents are configured primarily through the constructor:
Fluent Configuration
For runtime configuration changes, use setter methods:
π€ Return Formats
Agents support five return formats: single, all, json, xml, and raw.
Return Format Flow
Single (Default)
Agents default to "single" format, returning just the assistant's content as a string:
All Messages
Returns all messages including system, memory context, and response:
Raw Response
Returns the full provider response structure:
JSON Format
XML Format
Streaming Responses
Stream agent responses in real-time:
Pipeline Integration
Agents implement IAiRunnable, so they work in pipelines:
Chaining Agents
Advanced Patterns
Agent with Dynamic Tools
Agent Introspection
Inspect agent configuration at runtime using getConfig():
Conditional Agent Execution
Sub-Agents
Sub-agents allow you to create specialized agents that can be delegated to by a parent agent. When you register a sub-agent, it is automatically wrapped as an internal tool that the parent agent can invoke.
Creating Agents with Sub-Agents
Fluent Sub-Agent API
You can also add sub-agents using the fluent API:
Sub-Agent Management
Sub-Agents in Configuration
Sub-agent information is included in getConfig():
How Sub-Agents Work
When you add a sub-agent, it is automatically converted to a tool:
Tool Name:
delegate_to_{agent_name}(lowercase, special characters replaced with underscores)Tool Description: Includes the sub-agent's name and description
Tool Parameter: A
taskparameter for the query to delegate
The parent agent's AI model decides when to use the delegation tool based on the task context.
Event Interception
Agents fire events during execution:
π Agents with Document Loaders & RAG
Agents can leverage document loaders and vector memory to access knowledge bases and provide grounded, factual responses.
π Agent RAG Workflow
Basic RAG Agent
Create an agent with access to a knowledge base:
Multi-Source RAG Agent
Combine multiple knowledge bases:
RAG Agent with Real-Time Data Tools
Combine document retrieval with live data access:
Custom Context Injection
Manually inject specific context into agent queries:
Conditional Document Loading
Load documents based on user query:
π Agents with Transformers
Agents can use transformers to process their inputs and outputs for specialized workflows.
Output Transformation
Transform agent responses automatically:
Input Processing
Pre-process user input before sending to agent:
Structured Output from Agents
Use transformers to extract structured data from agent responses:
Multi-Stage Agent Processing
Chain multiple agents with transformers between them:
Best Practices
1. Provide Clear Instructions
2. Choose Appropriate Tools
3. Manage Memory Lifecycle
4. Set Appropriate Parameters
5. Handle Errors Gracefully
Real-World Examples
Customer Support Agent
Code Review Agent
Research Assistant
Next Steps
Explore Memory Systems for conversation history and context management
See Vector Memory for semantic search and RAG workflows
Learn about RAG Pipelines for complete document-to-answer workflows
See Document Loaders for loading data from various sources
Learn about Transformers for data processing in pipelines
See Message Context for injecting security and RAG data into agents
See Custom Memory for building custom memory implementations
Learn about Tools for function calling patterns
See Events for agent event handling
Check Pipeline Overview for advanced agent workflows
Last updated