Built-In Functions Reference
Reference documentation for built-in functions in BoxLang AI module
Complete reference documentation for all BoxLang AI built-in functions (BIFs). These functions provide the primary interface for AI operations in BoxLang.
📚 Overview
The BoxLang AI module provides 18 built-in functions organized into functional categories:
🗨️ Chat & Conversation
Core functions for AI chat interactions.
aiChat()- Synchronous AI chat with simple interfaceaiChatAsync()- Asynchronous chat returning FutureaiChatStream()- Streaming chat with real-time callbacksaiChatRequest()- Create reusable request objects
🤖 Agents & Models
Create autonomous agents and model runnables.
aiAgent()- Create AI agents with tools, memory, and reasoningaiModel()- Create AI model runnables for pipelinesaiService()- Get AI service provider instances
💾 Memory & Context
Manage conversation history and knowledge bases.
aiMemory()- Create memory instances (conversation, vector, cache, etc.)
📄 Documents & RAG
Load and process documents for RAG workflows.
aiDocuments()- Load documents with fluent APIaiChunk()- Chunk text into segmentsaiEmbed()- Generate vector embeddings
🔄 Transformation & Pipelines
Transform data in AI pipelines.
aiMessage()- Build message structures with fluent APIaiTransform()- Create transformation runnablesaiPopulate()- Populate classes from AI responses
🔧 Tools & Utilities
Extend AI capabilities and estimate costs.
aiTool()- Create callable tools for agentsaiTokens()- Estimate token counts and costs
🔌 MCP (Model Context Protocol)
Connect AI to external tools and data sources.
MCP()- Create MCP client for consuming serversMCPServer()- Create MCP server for exposing tools
🎯 Quick Reference
Common Usage Patterns
Simple Chat
Agent with Tools
RAG (Retrieval Augmented Generation)
Streaming Responses
Structured Output
📊 Function Categories by Use Case
For Simple AI Calls
Start with these for basic AI interactions:
aiChat()- Simplest sync chataiMessage()- Build complex messagesaiService()- Get provider instance
For Long-Running Operations
Use async/streaming for better UX:
aiChatAsync()- Non-blocking requestsaiChatStream()- Real-time responses
For Autonomous Behavior
Let AI reason and use tools:
aiAgent()- Autonomous agentsaiTool()- Create callable functionsaiMemory()- Maintain context
For Knowledge Bases (RAG)
Build AI that knows your data:
aiDocuments()- Load documentsaiMemory()- Vector storageaiEmbed()- Generate embeddingsaiChunk()- Split documents
For Pipelines
Chain AI operations:
aiModel()- Model runnablesaiTransform()- Data transformationaiMessage()- Fluent message building
For External Integration
Connect AI to external systems:
MCP()- Consume MCP serversMCPServer()- Expose tools via MCPaiTool()- Wrap any function
🔑 Key Concepts
Return Formats
All chat functions support multiple return formats:
"single": Just the content string (default for
aiChat())"all": Array of all messages
"raw": Complete API response with metadata
"json": Parsed JSON object
"xml": Parsed XML document
Class/Struct: Structured output (populate target)
Provider Selection
Three ways to specify AI provider:
Default: Uses module configuration
Parameter:
aiChat( msg, {}, { provider: "claude" } )Environment: Auto-detects
<PROVIDER>_API_KEYvariables
Memory Types
Different memory for different needs:
Window: Recent conversation (short-term)
Vector: Semantic search (RAG, knowledge)
Cache: Distributed storage (CacheBox)
File: Simple persistence
JDBC: Database-backed
Session: User session scope
Fluent APIs
Many functions return objects with chainable methods:
🎓 Learning Path
Beginner
Start with
aiChat()for simple requestsLearn
aiMessage()for structured conversationsTry
aiChatStream()for real-time responses
Intermediate
Create
aiAgent()with basic toolsUse
aiMemory()for conversation contextImplement
aiTool()for custom functions
Advanced
Build RAG systems with
aiDocuments()and vector memoryUse
aiChatAsync()for concurrent requestsCreate MCP servers with
MCPServer()Build complex pipelines with
aiModel()andaiTransform()
🔍 Function Index
📖 Additional Resources
Getting Started Guide - Introduction to BoxLang AI
Agents Documentation - Deep dive into agents
Memory Systems - Memory types and usage
RAG Guide - Build knowledge-based AI
Transformers - Data transformation
Examples - Working code examples
💡 Tips
Start simple: Begin with
aiChat()before moving to agentsUse appropriate memory: Window for chat, vector for knowledge
Clear tool descriptions: Help AI choose correct tools
Handle errors: Wrap AI calls in try/catch blocks
Monitor costs: Use
aiTokens()to estimate usageTest locally: Use Ollama for free local testing
Stream long responses: Better UX with
aiChatStream()Async for parallel: Use
aiChatAsync()for multiple concurrent requests
Last updated