Apiary Architecture

Overview

Apiary is an orchestration platform for AI agents (Hives) that provides declarative configuration, multi-tenancy, resource management, and advanced orchestration patterns. It follows a modular, decoupled architecture adhering to SOLID and DRY principles.

Core Components

1. Queen (Central Orchestrator)

The Queen is the central orchestrator that reconciles desired state (from manifests) with actual state (running agents).

Responsibilities:

Location: internal/queen/

Key Interfaces:

2. API Server

The API server provides both REST and gRPC APIs for managing Apiary resources.

Responsibilities:

Location: internal/api/

Endpoints:

3. Scheduler

The Scheduler allocates resources (CPU, memory) to agents based on availability and task tier hints.

Responsibilities:

Location: internal/scheduler/

Features:

4. Launcher

The Launcher manages agent process lifecycle.

Responsibilities:

Location: internal/launcher/

Process Management:

5. Nectar (Message Bus)

Nectar provides the message bus for agent-to-agent communication using NATS.

Responsibilities:

Location: internal/nectar/

Features:

6. Comb (Shared Memory)

Comb provides session-scoped shared memory for agents.

Responsibilities:

Location: internal/comb/

Features:

7. Keeper (Agent Sidecar)

The Keeper is a sidecar process that runs alongside each agent, providing infrastructure services.

Responsibilities:

Location: internal/keeper/

Features:

8. Store (BadgerDB)

The Store provides persistent storage for Apiary resources using BadgerDB.

Responsibilities:

Location: internal/store/badger/

Features:

9. Session Manager

The Session Manager manages session lifecycle.

Responsibilities:

Location: internal/session/

10. Autoscaler

The Autoscaler automatically scales agents based on metrics.

Location: internal/autoscaler/

11. Warm Pool Manager

The Warm Pool Manager maintains pools of pre-warmed agents for low-latency startup.

Location: internal/warmpool/

12. Guardrails

Guardrails enforce safety and limits on agent operations.

Components:

Location: internal/guardrails/

Data Flow

Agent Deployment Flow

  1. User creates/updates an AgentSpec manifest
  2. API Server validates and stores the AgentSpec
  3. Queen watches for changes and reconciles
  4. Queen requests resources from Scheduler
  5. Launcher starts agent process with Keeper sidecar
  6. Keeper connects to message bus and Comb
  7. Agent is ready to receive messages

Message Flow

  1. Message published to topic (via REST/gRPC API or another agent)
  2. Message bus (Nectar) routes to subscribers
  3. Keeper receives message and buffers it
  4. Keeper sends message to agent (stdin/HTTP)
  5. Agent processes message and responds
  6. Keeper publishes response to output topic
  7. Response flows back to caller or next stage in pipeline

Multi-Tenancy (Cells)

Cells provide namespace isolation in Apiary:

Orchestration Patterns

Apiary supports multiple orchestration patterns including Pipeline, Hierarchical, Swarm, Event-Driven, and Composed patterns.

Design Decisions