Skip to content

Self-improving AI Employees for everyone

22.0 KB
Raw Blame

Autonomous Agent Creation: A Comprehensive Guide

ARCHIVED 2026-09-04 — Superseded. External research notes (Symbolica, Cloudflare Code Mode, Anthropic, BoundaryML). Contains no OpenCompany file paths or symbols and never described this repo's autonomous_agent plugin.

For current state see:

Introduction

This document synthesizes insights from leading AI research organizations on building autonomous agents. The approaches discussed represent a paradigm shift from traditional tool-calling patterns toward code-centric agent architectures that dramatically improve efficiency, composability, and capabilities.

The core insight shared across all sources: code is the most expressive interface through which AI models can interact with their environment. LLMs were trained on vast amounts of code, making it their native language for expressing complex operations.


Table of Contents

  1. The Problem with Traditional Tool-Calling
  2. Code Mode: A New Paradigm
  3. Architecture Patterns
  4. Implementation Approaches
  5. Security and Sandboxing
  6. Multi-Agent Orchestration
  7. Practical Implementation Guide
  8. Performance Benchmarks
  9. Future Directions

The Problem with Traditional Tool-Calling

The While Loop Problem

Traditional agent frameworks operate through a tedious repetition pattern:

Developer specifies tools with parameters and return types
    -> LLM receives prompt instructing tool selection
    -> Client executes "the while loop":
        LLM call -> tool execution -> context update -> repeat

This approach has several fundamental limitations:

Problem Description
Token Overhead Each tool call requires round-trip communication, consuming tokens on every iteration
Context Window Bloat Tool definitions loaded upfront can consume 150,000+ tokens before addressing user requests
Intermediate Result Duplication Data retrieved by agents passes through the model multiple times
Limited Composability Single-step tool calls don't compose naturally into complex workflows
Framework Boilerplate Developers write both application logic and framework scaffolding simultaneously

The RPC Analogy

BoundaryML draws a useful parallel between tool-calling and Remote Procedure Calls:

Programming Concept Agentic Systems Equivalent
RPC Call Tool call
Programmer Agent
Runtime While loop
Variables Context stack

The critical insight: "The while loop is not a good runtime. The context stack is not a good binding environment."

Token Economics

Consider a practical example from Anthropic's research:

  • A 2-hour meeting transcript could require processing an additional 50,000 tokens when passed through the model multiple times
  • Standard MCP implementations loading all tool definitions upfront can consume hundreds of thousands of tokens before any useful work begins

Code Mode: A New Paradigm

Core Concept

Code Mode represents a fundamental shift where AI agents generate and execute code rather than making sequential tool calls. Both Cloudflare and Anthropic independently arrived at this conclusion in late 2025.

Traditional Approach:
    Agent -> Tool Call 1 -> Result 1 -> Agent -> Tool Call 2 -> Result 2 -> ...

Code Mode Approach:
    Agent -> Generate Code -> Execute in Sandbox -> Return Final Result

Why Code?

"Code, on the other hand, is what these models were trained on. It's their native language." — Rita Kozlov, VP of AI & Developers at Cloudflare

Key advantages:

  • Native expression format for LLMs
  • Built-in control flow (loops, conditionals, error handling)
  • Compositional by design (functions call functions)
  • Type-safe with proper annotations
  • Self-documenting through code structure

Progressive Tool Discovery

Rather than loading all tool definitions upfront, Code Mode presents tools as a navigable filesystem:

servers/
├── google-drive/
│   ├── getDocument.ts
│   └── index.ts
├── salesforce/
│   ├── updateRecord.ts
│   └── index.ts
└── calendar/
    ├── createEvent.ts
    ├── listEvents.ts
    └── index.ts

Agents discover tools by navigating this structure, loading only definitions needed for current tasks.

Result: Token usage reduced from 150,000 to approximately 2,000—a 98.7% savings.


Architecture Patterns

1. Dynamic Scope Management (Symbolica Agentica)

Agents operate within a dynamic scope—a collection of objects that evolves as the agent executes:

# Initial objects placed in scope at creation
scope = {
    'database': DatabaseClient(),
    'email': EmailService(),
}

# Objects returned by method calls automatically enter scope
user = scope['database'].get_user(123)
# 'user' now available with its own methods: update(), delete(), get_posts()

posts = user.get_posts()
# 'posts' collection now available for iteration

This pattern enables incremental capability discovery without preloading everything into context.

2. Object Hierarchies Over Flat Functions

Instead of exposing flat function lists, leverage object-oriented patterns:

# Flat approach (problematic)
tools = [
    get_user,
    update_user,
    delete_user,
    get_user_posts,
    create_user_post,
    # ... 50 more functions
]

# Object hierarchy approach (preferred)
class User:
    def update(self, **kwargs): ...
    def delete(self): ...
    def get_posts(self) -> List[Post]: ...

class Post:
    def update(self, **kwargs): ...
    def delete(self): ...
    def add_comment(self, text: str) -> Comment: ...

Benefits:

  • Natural capability encoding through method availability
  • Reduced context size (methods discovered on-demand)
  • Type-safe composition (IDE-like "go-to-definition" for agents)

3. Compositional Tools (BoundaryML)

Rather than single-step tools, provide compositional primitives:

Tool Purpose
sequence Execute ordered tool chains, returning final result
forEach Apply tools across collections
callBuiltin Execute standard functions
assignVariable Bind values for reuse
conditional Branch based on conditions

This enables agents to compose multi-step programs rather than single RPC calls.

4. Filesystem-Based Tool Organization (Anthropic MCP)

// servers/calendar/createEvent.ts
export interface CreateEventParams {
    title: string;
    startTime: Date;
    endTime: Date;
    attendees?: string[];
}

export async function createEvent(params: CreateEventParams): Promise<Event> {
    // Implementation
}

Agents can:

  • Browse available servers
  • Read tool definitions on-demand
  • Use search_tools functions for filtered discovery
  • Cache frequently-used definitions

Implementation Approaches

Approach 1: Code Generation with Sandboxed Execution

Used by: Cloudflare, Anthropic

// Agent generates code like this:
const events = [];
for (let i = 0; i < 31; i++) {
    const event = await calendar.createEvent({
        title: `Daily Standup - Day ${i + 1}`,
        startTime: addDays(startDate, i),
        duration: 30
    });
    events.push(event);
}
return events;

Execution flow:

  1. Agent receives MCP server schema
  2. Generates executable JavaScript/TypeScript
  3. Code executes in sandboxed V8 isolate (Cloudflare Workers)
  4. Results returned to agent context

Benefits:

  • 31 events created through single loop vs. 31 API calls
  • Native control flow (loops, conditionals, try/catch)
  • Type checking before execution

Approach 2: Agentica Framework Pattern

Used by: Symbolica

from agentica import spawn

# Third-party clients integrate directly
from twelvedata import TDClient

market_data = TDClient()
finance_agent = await spawn(
    system_prompt="You are a financial analysis assistant.",
    scope={'market_data': market_data}
)

# Agent can now use market_data methods directly in generated code
result = await finance_agent.run(
    "Analyze AAPL stock performance over the last quarter",
    return_type=FinancialReport  # Type-safe returns
)

Key features:

  • Library integration without wrapper functions
  • show_definition() for on-demand inspection
  • Type-safe return type enforcement
  • Sub-agent spawning via spawn() in scope

Approach 3: Compositional DSL Pattern

Used by: BoundaryML

{
  "type": "sequence",
  "steps": [
    {
      "type": "forEach",
      "items": { "ref": "attendees" },
      "body": {
        "type": "sequence",
        "steps": [
          {
            "type": "callBuiltin",
            "function": "calendar.getAvailability",
            "params": { "userId": { "ref": "item" } }
          },
          {
            "type": "assignVariable",
            "name": "availabilities",
            "value": { "append": { "ref": "result" } }
          }
        ]
      }
    },
    {
      "type": "callBuiltin",
      "function": "calendar.findCommonSlot",
      "params": { "availabilities": { "ref": "availabilities" } }
    }
  ]
}

Benefits:

  • Type-checkable and lintable
  • No per-step LLM callbacks
  • Clear program structure
  • Generalizes from examples (2-person to N-person scheduling)

Security and Sandboxing

Multi-Layer Security Architecture

Production code execution requires robust isolation:

┌─────────────────────────────────────────────┐
│                 User Space                   │
│  ┌─────────────────────────────────────┐    │
│  │            Application               │    │
│  │  ┌─────────────────────────────┐    │    │
│  │  │      WASM Sandbox           │    │    │
│  │  │  ┌───────────────────┐      │    │    │
│  │  │  │   Agent Code      │      │    │    │
│  │  │  └───────────────────┘      │    │    │
│  │  └─────────────────────────────┘    │    │
│  └─────────────────────────────────────┘    │
│                                              │
│              MicroVM Boundary                │
└─────────────────────────────────────────────┘

Symbolica's Approach: Remote Object Proxying

# Agent code executes in sandbox
# Objects remain local to user
# Method calls trigger RPC across process boundaries

class RemoteProxy:
    def __init__(self, object_id: str, rpc_client: RPCClient):
        self._id = object_id
        self._rpc = rpc_client

    def __getattr__(self, name):
        async def method_call(*args, **kwargs):
            return await self._rpc.call(self._id, name, args, kwargs)
        return method_call

Cloudflare's Approach: V8 Isolates

  • Code executes in sandboxed V8 isolate
  • Powered by Workers Loader
  • Negligible cold start due to Workers infrastructure
  • Code Executor binding handles script execution
  • Creation IDs for monitoring and tracking

Security Checklist

Concern Mitigation
Code Injection Sandbox execution environment
Resource Exhaustion CPU/memory limits per execution
Data Exfiltration Network restrictions, allowlists
API Key Exposure Keys never enter model context
Sensitive Data Leakage Automatic PII tokenization
Infinite Loops Execution timeouts
File System Access Virtual filesystem isolation

Privacy-Preserving Execution

Anthropic's approach to sensitive data:

// Intermediate data remains in execution environment
// PII automatically tokenized before entering model context

const spreadsheet = await drive.getSpreadsheet("salary-data.xlsx");
// 10,000 rows with PII

const summary = spreadsheet
    .filter(row => row.department === "Engineering")
    .aggregate({ avgSalary: "mean(salary)" });
// Only aggregate returned to model context

Multi-Agent Orchestration

Agentica-in-Scope Pattern

Placing the spawn() function in an agent's scope enables autonomous multi-agent orchestration:

# Orchestrator agent can spawn sub-agents dynamically
orchestrator = await spawn(
    "You orchestrate complex research tasks.",
    scope={
        'spawn': spawn,  # Enable sub-agent creation
        'search': SearchClient(),
        'summarize': SummarizationService(),
    }
)

# Orchestrator can now create specialized sub-agents
# Sub-agents inherit capabilities and objects from orchestrator

Type-Safe Agent Composition

# Agents specify return types when invoked
# Execution environment enforces type compliance

research_result = await research_agent.run(
    "Research quantum computing trends",
    return_type=ResearchReport
)

# Subsequent agents can reliably depend on returned objects
analysis = await analysis_agent.run(
    f"Analyze this report: {research_result}",
    return_type=AnalysisSummary,
    scope={'report': research_result}  # Type-safe injection
)

Agent Communication Patterns

Pattern Description Use Case
Sequential Pipeline Output of agent N feeds input of agent N+1 Document processing workflows
Parallel Fan-Out Multiple agents process simultaneously Research aggregation
Hierarchical Delegation Parent spawns child agents for subtasks Complex problem decomposition
Shared State Agents share objects through scope Collaborative editing

Practical Implementation Guide

Step 1: Define Your Tool Schema

// servers/crm/index.ts
export interface CRMServer {
    contacts: {
        list(filters?: ContactFilters): Promise<Contact[]>;
        get(id: string): Promise<Contact>;
        create(data: CreateContactData): Promise<Contact>;
        update(id: string, data: UpdateContactData): Promise<Contact>;
    };
    deals: {
        list(filters?: DealFilters): Promise<Deal[]>;
        create(data: CreateDealData): Promise<Deal>;
        updateStage(id: string, stage: DealStage): Promise<Deal>;
    };
}

Step 2: Implement Object Hierarchies

class Contact:
    """CRM Contact with related operations."""

    def __init__(self, data: dict, client: CRMClient):
        self.id = data['id']
        self.name = data['name']
        self.email = data['email']
        self._client = client

    async def get_deals(self) -> List['Deal']:
        """Get all deals associated with this contact."""
        return await self._client.deals.list(contact_id=self.id)

    async def create_deal(self, data: CreateDealData) -> 'Deal':
        """Create a new deal for this contact."""
        data['contact_id'] = self.id
        return await self._client.deals.create(data)

    async def send_email(self, subject: str, body: str) -> EmailResult:
        """Send email to this contact."""
        return await self._client.email.send(
            to=self.email,
            subject=subject,
            body=body
        )

Step 3: Configure Sandbox Environment

// Cloudflare Workers approach
export default {
    async fetch(request: Request, env: Env): Promise<Response> {
        const agent = new Agent({
            model: "claude-4",
            servers: {
                crm: new CRMServer(env.CRM_API_KEY),
                email: new EmailServer(env.EMAIL_API_KEY),
            },
            sandbox: {
                maxExecutionTime: 30000,  // 30 seconds
                maxMemory: 128 * 1024 * 1024,  // 128MB
                allowedNetworkHosts: ['api.crm.com', 'api.email.com'],
            }
        });

        return agent.handle(request);
    }
};

Step 4: Enable Progressive Discovery

class AgentScope:
    """Dynamic scope with progressive discovery."""

    def __init__(self, initial_objects: dict):
        self._objects = initial_objects
        self._definitions_cache = {}

    def show_definition(self, object_name: str) -> str:
        """IDE-like go-to-definition for agents."""
        if object_name not in self._definitions_cache:
            obj = self._objects.get(object_name)
            if obj:
                self._definitions_cache[object_name] = self._extract_definition(obj)
        return self._definitions_cache.get(object_name, "Object not found")

    def _extract_definition(self, obj) -> str:
        """Extract type hints, docstrings, and method signatures."""
        import inspect
        lines = []
        for name, method in inspect.getmembers(obj, predicate=inspect.ismethod):
            if not name.startswith('_'):
                sig = inspect.signature(method)
                doc = method.__doc__ or "No description"
                lines.append(f"{name}{sig}: {doc}")
        return "\n".join(lines)

Step 5: Implement Type-Safe Returns

from pydantic import BaseModel
from typing import TypeVar, Generic

T = TypeVar('T', bound=BaseModel)

class TypedAgentResult(Generic[T]):
    def __init__(self, value: T, execution_trace: List[str]):
        self.value = value
        self.trace = execution_trace

async def run_agent(
    prompt: str,
    return_type: Type[T],
    scope: dict
) -> TypedAgentResult[T]:
    """Execute agent with type-safe return enforcement."""

    # Generate and execute code
    result = await execute_in_sandbox(prompt, scope)

    # Validate return type
    if not isinstance(result.value, return_type):
        try:
            result.value = return_type.model_validate(result.value)
        except ValidationError as e:
            raise AgentTypeError(f"Return type mismatch: {e}")

    return result

Performance Benchmarks

Token Efficiency Comparison

Scenario Traditional MCP Code Mode Savings
Simple single-event task 1,000 tokens 680 tokens 32%
31-event creation 15,500 tokens 2,945 tokens 81%
Tool definition loading 150,000 tokens 2,000 tokens 98.7%
Complex workflow (10 steps) 25,000 tokens 4,500 tokens 82%

Benchmark Results

Symbolica Agentica on BrowseComp-Plus:

  • GPT-5 with Agentica: 77.11%
  • GPT-5 without Agentica: 73.25%
  • Improvement: 3.86 percentage points

Latency Improvements

Operation Sequential Tool Calls Code Mode
31 calendar events ~31 round trips 1 round trip
Data filtering (10K rows) N/A (context overflow) Single execution
Multi-step workflow N * LLM latency 1 * LLM latency

Future Directions

Research Areas

  1. Better DSLs for Agent Programs

    • Moving beyond single RPC calls toward coherent program structures
    • Type-checking and linting for agent-generated plans
    • Formal verification of agent behavior
  2. Context Degradation Mitigation

    • Research shows context degradation in extended conversations
    • LLMs perform better with complete program specifications
    • Single-shot program generation vs. multi-turn accumulation
  3. Skill Learning and Persistence

    • Agents save reusable code functions as recoverable skills
    • State persistence across operations through filesystem access
    • Learning from successful execution traces
  4. Cross-Language Agent Interoperability

    • Language-agnostic object models
    • Runtime type checking across language boundaries
    • Unified execution environments (Python, TypeScript, more)

Emerging Patterns

Pattern Description
Agent Skills Persistent, reusable code snippets agents can invoke
Typed Agent Protocols Formal interface definitions for agent communication
Execution Traces Debuggable records of agent decisions and code
Rollback Capabilities Transaction-like semantics for agent operations

Conclusion

The shift from traditional tool-calling to code-centric agent architectures represents a fundamental advancement in AI agent design. Key takeaways:

  1. Code is the natural interface for LLMs to express complex operations
  2. Progressive discovery dramatically reduces token consumption
  3. Object hierarchies enable natural capability encoding
  4. Sandboxed execution provides necessary security guarantees
  5. Type-safe composition enables reliable multi-agent systems

The convergence of Cloudflare, Anthropic, Symbolica, and BoundaryML on these patterns suggests a maturing consensus on how to build production-ready autonomous agents.


References


Document created: 2026-02-12 Based on research from Symbolica, Cloudflare, Anthropic, and BoundaryML