Skip to main content

Overview

In OpenFang, an agent is a persistent conversational entity with its own identity, memory, capabilities, and model configuration. Unlike stateless LLM wrappers, OpenFang agents maintain long-running sessions, build knowledge graphs, and can spawn child agents.
Agents are first-class citizens in OpenFang. They have IDs, manifests, budgets, permissions, and appear in the kernel’s agent registry.

Agent Manifest

Every agent is defined by an AgentManifest that declares:
Capabilities define what an agent can do. Examples:
The kernel checks capabilities before every tool invocation. If an agent tries to use file_write without FileWrite capability, the call is rejected with an audit trail entry.

The Agent Loop

When a user sends a message to an agent, the kernel invokes the agent loop — the core execution engine in openfang-runtime/src/agent_loop.rs.
1

Session Retrieval

Load the agent’s conversation history from the memory substrate. If this is the first message, create a new session.
2

Memory Recall

Query the semantic store for relevant past memories using vector similarity search (if embedding driver available) or full-text search.
3

Prompt Construction

Build the final LLM prompt by combining:
  • System prompt from manifest
  • Recalled memories (appended as context)
  • Session message history
  • Tool definitions (if agent has tool-use capability)
  • User’s new message
4

LLM Invocation

Call the LLM driver (Anthropic, Gemini, or OpenAI-compatible) with the constructed prompt. Stream tokens back as they arrive.
5

Tool Execution

If the LLM returns tool calls, execute each one:
  • Capability check: Verify agent has permission for this tool
  • Taint check: Ensure no untrusted data flows into privileged operations
  • WASM sandbox: If the tool is a custom skill, run it in the WASM sandbox with fuel metering
  • Timeout enforcement: Kill tools that run longer than 120 seconds
  • Audit logging: Record every tool invocation in the Merkle hash chain
Return tool results as new messages and loop back to step 4.
6

Iteration Guard

The loop runs for up to 50 iterations (or the agent’s max_iterations override). This prevents infinite loops and runaway costs.
7

Response Assembly

Extract the final text response, apply reply directives (e.g., [[silent]], [[delay:5s]]), and return to the kernel.
8

Session Persistence

Save the updated message history and usage statistics back to the memory substrate.
The agent loop is synchronous per agent — only one message at a time per agent. This prevents session corruption from concurrent writes. Multiple agents can run in parallel.

Loop Guards & Safety

OpenFang includes multiple layers of protection against runaway agents:

Loop Guard (Circuit Breaker)

Detects when an agent is stuck in a tool-call loop:
When triggered, the guard injects a system message:
LOOP DETECTED: You have called the same tool 5 times in a row. Consider a different approach or provide a final answer.

Fuel Metering (WASM Sandbox)

Custom skills run in a WebAssembly sandbox with dual metering:
  1. Fuel budget: Deterministic instruction counting (default: 1,000,000 fuel units)
  2. Epoch interruption: Wall-clock timeout with watchdog thread (default: 30 seconds)
If either limit is exceeded, the skill is killed with SandboxError::FuelExhausted.

Context Overflow Recovery

If an LLM call fails with context_length_exceeded, OpenFang automatically:
  1. Trims oldest messages from history
  2. Truncates long tool results
  3. Compresses memory fragments
  4. Retries with reduced context
This happens transparently — users see a brief [context overflow, recovering...] notice.

Agent Lifecycle States

Agent Hierarchy

Agents can spawn child agents if they have the AgentSpawn capability:
Privilege inheritance rules:
  • Child agents inherit a subset of parent capabilities (never more)
  • Budget limits are shared (child spending counts against parent)
  • Killing a parent cascades to all children
  • Session memory is isolated (children can’t read parent’s messages)

Scheduling & Cron

Agents can run on schedules using the kernel’s cron subsystem:
1

Register Schedule

This creates a cron entry that messages the researcher agent every weekday at 9 AM.
2

Background Executor

The kernel runs a background task that checks schedules every minute. When a schedule matches, it enqueues a message to the agent.
3

Execution

The agent processes the scheduled message through the normal agent loop. The response can be delivered via configured channels (Telegram, email, etc.).
Hands (autonomous capability packages) use this scheduling system to run 24/7. The Researcher Hand, for example, might run hourly to monitor competitor websites.

Budget Tracking

Every agent has a token usage and cost budget:
After every LLM call, the kernel:
  1. Calculates cost using the model catalog pricing
  2. Updates the agent’s budget
  3. Checks if limits are exceeded
  4. Optionally triggers alerts or pauses the agent

Observability

The kernel emits structured logs and metrics for every agent operation:
Audit trail records:
  • Every agent spawn/kill
  • Every tool invocation
  • Every capability check (pass/fail)
  • Every memory access
Query the audit log:

Next Steps

Hands System

Learn about autonomous agents that run on schedules

Memory Substrate

Explore how agents store and recall information

Security

Understand capability checks and sandboxing

API Reference

Full API docs for agent management