Skip to main content

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
Never commit your .env file to version control. Add it to .gitignore!

Understanding Core Concepts

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.)
Agents can work independently or collaborate with other agents in coordinated workflows.
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.
Tasks can reference outputs from previous tasks to create complex workflows.
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.
Orbits manage the entire execution lifecycle and provide detailed metrics.

Step-by-Step Implementation

Step 1: Initialize OrbitAI

Create your main entry point in Sources/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:
Temperature Settings: Use lower values (0.1-0.3) for structured tasks like list organization, and higher values (0.7-0.9) for creative tasks like meal planning.

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

Enhance your agents with persistent memory and knowledge sources:
Memory enables agents to learn from past interactions and maintain context across sessions.
Create custom tools for specialized functionality:
Custom tools extend agent capabilities with domain-specific functionality.
Use structured output for JSON data:
Structured output ensures consistent, parseable results from your agents.
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

Problem: Invalid API key or authentication failed.Solution: Check your environment variables:
Ensure the .env file is in the correct directory and properly formatted.
Problem: Task execution exceeded maximum time limit.Solution: Increase the timeout or simplify the task:
Problem: Unable to process large outputs.Solution: Use streaming or chunked processing:
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.