Build Your First Agent from Scratch
Welcome to OrbitAI! This guide will walk you through building a complete multi-agent system using a practical recipe preparation workflow. Youβll create specialized AI agents that work together to plan meals, find recipes, and generate shopping lists.Meal Planner
Analyzes dietary preferences and creates personalized meal plans
Recipe Researcher
Finds and analyzes recipes for planned meals
Shopping List
Generates organized shopping lists from recipes
Installation & Setup
1
Create a New Swift Package
Initialize a new Swift executable package for your recipe prep system:
2
Add OrbitAI Dependency
Edit your
Package.swift file to include OrbitAI:Package.swift
3
Configure Environment Variables
Create a
.env file in your project root with your API credentials:.env
Understanding Core Concepts
Agents
Agents
Agents are autonomous AI entities with specific roles and capabilities:
- Role: What the agent represents (e.g., βMeal Plannerβ).
- Purpose: The agentβs primary goal.
- Context: Background information that shapes behavior.
- Tools: Available capabilities (web search, read files, etc.)
Tasks
Tasks
Tasks define specific work to be completed:
- Description: What needs to be done.
- Expected Output: Format and content requirements.
- Agent Assignment: Which agent handles the task.
- Dependencies: Tasks that must complete first.
Orbits
Orbits
Orbits orchestrate agents and tasks into workflows:
- Process Type: Sequential (one after another) or Hierarchical (manager delegates).
- Execution Flow: How tasks are coordinated.
- Resource Management: Memory, knowledge bases, tool access.
Step-by-Step Implementation
Step 1: Initialize OrbitAI
Create your main entry point inSources/RecipePrepOrbit/main.swift:
main.swift
The LLM manager handles all communication with AI providers, including caching, metrics, and error handling.
Step 2: Create Specialized Agents
Define three specialized agents for your workflow:- Meal Planner Agent
- Recipe Research Agent
- Shopping List Agent
Step 3: Define Workflow Tasks
Create tasks with proper dependencies:Tasks can reference previous task outputs using
{task_N_output} syntax in their descriptions. This enables powerful chaining of agent workflows.Step 4: Create and Execute the Orbit
Bring everything together by creating the orbit:Step 5: Build and Run
Execute your first agent workflow:Expected Output
When you run the application, youβll see:Advanced Features
Memory and Knowledge Bases
Memory and Knowledge Bases
Enhance your agents with persistent memory and knowledge sources:Memory enables agents to learn from past interactions and maintain context across sessions.
Custom Tools
Custom Tools
Create custom tools for specialized functionality:Custom tools extend agent capabilities with domain-specific functionality.
Structured Output
Structured Output
Use structured output for JSON data:Structured output ensures consistent, parseable results from your agents.
Performance Monitoring
Performance Monitoring
Track detailed metrics and performance:Metrics help you optimize performance and track resource usage.
Best Practices
Agent Design
- Single Responsibility: One clear purpose per agent.
- Clear Context: Detailed background information.
- Appropriate Tools: Only assign needed tools.
- Temperature: Lower for structured, higher for creative.
Task Definition
- Be Specific: Explicit descriptions.
- Define Output: Clear format and structure.
- Set Constraints: Realistic time limits.
- Use Dependencies: Chain task outputs.
Error Handling
- Try-Catch Blocks: Handle specific errors.
- Retry Logic: Implement for transient failures.
- Graceful Degradation: Fallback strategies.
- Verbose Logging: Debug mode for troubleshooting.
Resource Management
- Rate Limiting: Prevent API throttling.
- Timeouts: Set appropriate limits.
- Caching: Reduce redundant API calls.
- Metrics: Track usage and costs.
Troubleshooting
Authentication Errors
Authentication Errors
Problem: Invalid API key or authentication failed.Solution: Check your environment variables:Ensure the
.env file is in the correct directory and properly formatted.Task Timeout Issues
Task Timeout Issues
Problem: Task execution exceeded maximum time limit.Solution: Increase the timeout or simplify the task:
Memory Issues
Memory Issues
Problem: Unable to process large outputs.Solution: Use streaming or chunked processing:
Tool Execution Failures
Tool Execution Failures
Problem: Tool execution failed.Solution: Check tool availability:
Next Steps
Congratulations on building your first OrbitAI agent system! Hereβs what to explore next:Agents
Learn about Agents, Tasks, Orbits, and more
Agent Examples
Browse practical agent implementations
Need help? Join our GitHub Discussions or check out the Issue Tracker for support.