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 anAgentManifest that declares:
Capability-Based Permissions
Capability-Based Permissions
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 inopenfang-runtime/src/agent_loop.rs.
Session Retrieval
Load the agent’s conversation history from the memory substrate. If this is the first message, create a new session.
Memory Recall
Query the semantic store for relevant past memories using vector similarity search (if embedding driver available) or full-text search.
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
LLM Invocation
Call the LLM driver (Anthropic, Gemini, or OpenAI-compatible) with the constructed prompt. Stream tokens back as they arrive.
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
Iteration Guard
The loop runs for up to 50 iterations (or the agent’s
max_iterations override). This prevents infinite loops and runaway costs.Response Assembly
Extract the final text response, apply reply directives (e.g.,
[[silent]], [[delay:5s]]), and return to the kernel.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: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:- Fuel budget: Deterministic instruction counting (default: 1,000,000 fuel units)
- Epoch interruption: Wall-clock timeout with watchdog thread (default: 30 seconds)
SandboxError::FuelExhausted.
Context Overflow Recovery
If an LLM call fails withcontext_length_exceeded, OpenFang automatically:
- Trims oldest messages from history
- Truncates long tool results
- Compresses memory fragments
- Retries with reduced context
[context overflow, recovering...] notice.
Agent Lifecycle States
State Transitions in Code
State Transitions in Code
Agent Hierarchy
Agents can spawn child agents if they have theAgentSpawn capability:
- 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)
Spawning a Child Agent in Code
Spawning a Child Agent in Code
Scheduling & Cron
Agents can run on schedules using the kernel’s cron subsystem:Register Schedule
researcher agent every weekday at 9 AM.Background Executor
The kernel runs a background task that checks schedules every minute. When a schedule matches, it enqueues a message to the agent.
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:- Calculates cost using the model catalog pricing
- Updates the agent’s budget
- Checks if limits are exceeded
- Optionally triggers alerts or pauses the agent
Observability
The kernel emits structured logs and metrics for every agent operation:- Every agent spawn/kill
- Every tool invocation
- Every capability check (pass/fail)
- Every memory access
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