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:

  1. Tool Name: delegate_to_{agent_name} (lowercase, special characters replaced with underscores)

  2. Tool Description: Includes the sub-agent's name and description

  3. Tool Parameter: A task parameter 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

Last updated