Level 5 — Autonomous Pattern: The LLM Writes and Runs Its Own Code

~12 min read

The most advanced level: the LLM generates and executes new code independently, effectively acting as its own developer — maximum autonomy, and correspondingly the highest risk and the least human oversight of any level.

Level 5, the Autonomous pattern, is the most advanced level in the progression, and it represents a genuinely different kind of capability than the levels below it. Here, the LLM generates and executes new code independently, effectively acting as an independent AI developer — rather than choosing among human-defined paths (Level 2), calling human-defined tools (Level 3), or coordinating human-defined sub-agents (Level 4), the model is now writing NEW code to accomplish whatever the task requires, and running that code itself.

This is a qualitatively different kind of autonomy: at every level below Level 5, a human has predefined the space of possible actions (the paths, the tools, the sub-agent roles) — the LLM makes decisions within that predefined space, but it can't do anything genuinely outside it. At Level 5, that constraint is gone: if the task needs a capability that doesn't already exist as a tool, the model can write the code to create that capability on the fly, rather than being limited to whatever a human anticipated and pre-built.

This maximal flexibility comes with correspondingly maximal risk. Every level below Level 5 has a human-defined boundary around what the system CAN do, even if the model's specific choice within that boundary is unpredictable. At Level 5, the boundary itself is far looser — code the model writes and executes can, in principle, do anything code can do, which makes sandboxing, execution limits, resource caps, and careful monitoring far more critical than at any lower level. This is exactly why the guardrails building block (covered elsewhere in this curriculum — tool-usage limits, validation checkpoints, fallback mechanisms) matters most at this level of autonomy, not least.

In practice, Level 5 autonomous systems are used carefully and usually within a sandboxed execution environment — data analysis agents that write and run their own analysis code, coding assistants that write and test their own implementations, or research agents that write custom scripts to process data in ways no pre-built tool anticipated. The autonomy that makes this level so capable is the exact same property that makes it the level requiring the most careful operational safeguards.

💻 Code example

import subprocess
import tempfile
from openai import OpenAI

client = OpenAI()

def level5_autonomous_agent(task: str) -> str:
    """Level 5: the LLM WRITES new code to accomplish the task, rather
    than choosing among pre-built tools. Sandboxing (timeout, restricted
    permissions) is essential here — this is illustrative, not
    production-hardened."""
    resp = client.chat.completions.create(model="gpt-4.1", messages=[
        {"role": "user", "content":
            f"Write a short, self-contained Python script (no external "
            f"dependencies) that accomplishes: {task}. Return ONLY the code."}
    ])
    generated_code = resp.choices[0].message.content

    with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
        f.write(generated_code)
        script_path = f.name

    # In production: run this in a locked-down sandbox with strict
    # timeouts, no network access, and resource limits — never like this.
    result = subprocess.run(
        ["python3", script_path], capture_output=True, text=True, timeout=5,
    )
    return result.stdout or result.stderr

print(level5_autonomous_agent("compute the first 10 Fibonacci numbers and print them"))

💬 Deep Dive with AI

Key points

  • Level 5 is the most advanced level: the LLM generates and executes new code independently, acting as its own developer
  • Unlike every level below it, the LLM isn't limited to a human-predefined space of paths, tools, or sub-agent roles
  • This maximal flexibility comes with maximal risk — code the model writes and runs can, in principle, do anything code can do
  • Sandboxing, execution limits, and monitoring matter most at this level, exactly because the boundary on what the system can do is far looser
  • Used carefully in practice — data analysis agents, coding assistants, research agents writing custom scripts within sandboxed environments