DocsHow the Agent Thinks
Internals

How the Agent Thinks

Overview of Krnl's reasoning model, prompt construct, sandboxed tool adapter execution, and memory design.

High-Level Architecture

Each Krnl agent consists of a structured prompt pipeline, configured tool adapters, short-term/long-term memory stores, and a deterministic execution runtime. The runtime constructs prompt states, executes tools safely, and logs outputs.

Prompt Pipeline

Structured system prompt with Observe-Plan-Act-Reflect phases, context injection, and tool schemas

Tool Adapters

Sandboxed execution layer for file operations, shell commands, HTTP requests, and more

Memory System

Short-term context for current session + long-term structured memory across sessions

Execution Runtime

Deterministic state machine with approval gates, rollback, and trace logging

Prompt Pipeline (Observe-Plan-Act-Reflect)

The agent reasons in four distinct phases, each with specific responsibilities:

1. Observe— Context Collection

Collects project context: active file statuses, recent commit trees, compiler logs, diagnostics, and user commands. Builds a mental model of the current state.

2. Plan— Strategy Generation

Generates a list of logical steps with explicit dependencies. Plans are validated by the runtime before starting execution. User approval required for destructive actions.

3. Act— Tool Execution

Executes plan steps using local sandboxed tool adapters (shell executor, file editor, HTTP clients). Each tool call is logged and can be rolled back.

4. Reflect— Verification & Summary

Compiles final results, verifies test suites, drafts explanations, records memory entries, and generates trace logs for auditability.

Live Example

Watch the agent think through a task: “add rate limiting to the API endpoint”

terminal
$ krnl
● Observe: Reading src/api/main.py...
● Observe: Found 3 endpoints, no rate limiting
● Observe: Dependencies: fastapi, redis (available)
● Plan: Add rate limiting
● Step 1: Install redis-rate-limit package
● Step 2: Add RateLimiter middleware to main.py
● Step 3: Apply to /api/* routes
● Step 4: Test with curl
● Act: Installing redis-rate-limit...
● Act: Editing src/api/main.py...
● Act: Running tests...
● Reflect: Rate limiting added successfully
● Reflect: 100 requests/minute limit configured
● Reflect: All tests passing
✓ Task completed in 4 steps

Tool Model & Adapters

Tools are structured modules with explicit schemas defining inputs and outputs. The runtime validates all outputs and prevents the execution of arbitrary commands unless authorized in `AGENT.md`.

Available Tools

File Operations

read_file, write_file, edit_file, list_dir, search_files — with path validation and backup

Shell Execution

run_command — with timeout, output capture, and approval for destructive commands

HTTP & Web

web_fetch, web_search — with rate limiting and content extraction

Memory & State

memory_write, memory_query — structured fact storage and retrieval

Safety First: All tool executions are logged. Destructive operations (rm, git push, etc.) require explicit user approval. Path constraints prevent access outside the workspace.

Memory & Context

The memory system divides data into two layers for optimal performance and persistence:

Short-term Context
  • • Active file contents and diagnostics
  • • Recent tool outputs and errors
  • • Current plan state and progress
  • • Conversation history (compacted)
  • • Ephemeral — cleared after session
Long-term Memory
  • • Project facts and conventions
  • • Architecture decisions
  • • Error and solution records
  • • User preferences and constraints
  • • Persistent — survives sessions

Safety Layers

Safety checks are executed at three key tiers to prevent unintended actions:

Tier 1: Input Validation

All tool inputs are validated against schemas. Path traversal attacks are blocked. Command injection is prevented.

Tier 2: Approval Gates

Destructive operations require user approval. The agent shows proposed changes before executing. Rollback is always available.

Tier 3: Audit Logging

Every action is logged with timestamp, context, and outcome. Trace logs enable debugging and compliance auditing.

Security Tip: Use path constraints and disable `--allow-network` for agents that don't need external connections to prevent data egress.

Complete Example Trace

Here is a complete trace log representing a task from start to finish:

terminal
$ krnl run "fix the failing user auth test"
● Observe: Reading workspace...
● Observe: Found 2 failing tests in tests/test_auth.py
● Observe: Error: AssertionError in test_login_invalid_credentials
● Plan: Fix auth test failures
● Step 1: Read test file to understand failure
● Step 2: Read auth implementation
● Step 3: Identify bug in password validation
● Step 4: Fix the bug
● Step 5: Run tests to verify
● Act: read_file tests/test_auth.py
● Act: read_file src/auth/login.py
● Act: Identified bug: missing password hash comparison
● Act: edit_file src/auth/login.py
● Added: if not verify_password(password, user.password_hash)
● Act: run_command pytest tests/test_auth.py
● Output: 2 tests passed
● Reflect: Bug fixed successfully
● Reflect: Tests now passing (2/2)
● Reflect: Memory updated: "password validation requires hash comparison"
✓ Task completed in 5 steps