KODE SDK Documentation

repository·main·Indexed 18 days ago

https://github.com/shareai-lab/kode-agent-sdk

An event-driven, long-running AI Agent development framework for enterprise-grade applications. Version 2.7.5 features multi-agent collaboration, sandboxed code execution, and robust persistence via JSONStore, SqliteStore, and PostgresStore. It supports providers including Anthropic, OpenAI, and Gemini, and utilizes a three-channel event system (Progress, Control, Monitor) and a Breakpoint State Machine for crash recovery.

Tokens
87K
Snippets
233
Records
301
Agent score
64%

What's inside KODE SDK

  1. What is KODE SDK?

    main

    KODE SDK is an Agent Runtime Kernel designed to manage the complete lifecycle of AI agents. It acts as a runtime layer (similar to how V8 manages JavaScript) that provides:

    • Agent lifecycle management: Create, run, pause, resume, and fork agents.
    • State persistence: Crash recovery using Write-Ahead Logging (WAL) protection.
    • Tool execution: Managed execution with permission governance.
    • Observability: A three-channel event system for monitoring and control.

    Note: KODE SDK does not handle HTTP routing, user authentication, multi-tenancy, or horizontal scaling; these must be architected in your application layer.

  2. Monitor AgentRuntimeState and BreakpointState

    main

    To track the lifecycle and execution progress of an agent, monitor AgentRuntimeState and BreakpointState.

    AgentRuntimeState describes the high-level status:

    • READY: Agent is idle.
    • WORKING: Agent is processing a message.
    • PAUSED: Agent is waiting for a permission decision.

    BreakpointState provides granular execution steps:

    • READY, PRE_MODEL, STREAMING_MODEL, TOOL_PENDING, AWAITING_APPROVAL, PRE_TOOL, TOOL_EXECUTING, POST_TOOL.
    type AgentRuntimeState = 'READY' | 'WORKING' | 'PAUSED';
    
    type BreakpointState =
      | 'READY'
      | 'PRE_MODEL'
      | 'STREAMING_MODEL'
      | 'TOOL_PENDING'
      | 'AWAITING_APPROVAL'
      | 'PRE_TOOL'
      | 'TOOL_EXECUTING'
      | 'POST_TOOL';
  3. Handle Agent Permissions in Electron

    main

    To implement tool approval and governance in a desktop app, use the following pattern:

    1. Main Process: Listen for the permission_required event on the agent. When triggered, send the tool details (name, input preview, call ID) to the Renderer via IPC.
    2. Renderer Process: Display an approval dialog to the user.
    3. Main Process: Provide an IPC handler (agent:permission-respond) that calls agent.decide(callId, decision, note) to allow or deny the tool execution based on user input.

    Decision options: 'allow' | 'deny'

    // Main Process: Listen for permission requests
    ipcMain.on('agent:permission-subscribe', (event, { agentId }) => {
      const agent = pool.get(agentId);
      if (!agent) return;
    
      agent.on('permission_required', async (permEvent) => {
        mainWindow.webContents.send(`agent:permission:${agentId}`, {
          callId: permEvent.call.id,
          toolName: permEvent.call.name,
          input: permEvent.call.inputPreview,
        });
      });
    });
    
    // Main Process: Respond to permission decisions
    ipcMain.handle('agent:permission-respond', async (event, { agentId, callId, decision, note }) => {
      const agent = pool.get(agentId);
      if (!agent) return { error: 'Agent not found' };
    
      await agent.decide(callId, decision, note);
      return { success: true };
    });
  4. Compare SQLite and PostgreSQL for KODE SDK

    main

    Choosing the right backend depends on your deployment scale and requirements.

    When to Choose SQLite

    • Use Case: Development, Single Instance, Quick prototyping.
    • Scale: Less than 1000 Agents and less than 100K messages per day.
    • Pros: Zero config, file-based, low maintenance.

    When to Choose PostgreSQL

    • Use Case: Production, Multi-Instance, High availability.
    • Scale: More than 1000 Agents and more than 100K messages per day.
    • Pros: Concurrent writes, complex JSONB queries, optimized for large datasets.
  5. Implement Scheduling and System Reminders

    main

    Agents can perform periodic tasks or send non-intrusive reminders using the built-in scheduler.

    • Step-based Scheduling: Use agent.schedule().everySteps(N, callback) to trigger logic every N steps.
    • System Reminders: Use agent.remind(text, options) to send reminders via the Monitor. These are separate from the Progress stream and do not clutter the main agent output.
    • External Triggers: For high-frequency tasks or integration with external Cron jobs, use scheduler.notifyExternalTrigger to wake the agent.
    • Todo Management: Use remindIntervalSteps in a Todo configuration to ensure periodic reviews of pending tasks.
  6. Choose a deployment pattern for KODE SDK

    main

    Select a deployment architecture based on your scale and environment requirements:

    1. Pattern 1: Single Process

      • Best for: CLI tools, Electron apps, VSCode extensions.
      • Architecture: The application runs the AgentPool and a local JSONStore in a single process, persisting data to local files.
    2. Pattern 2: Single Server

      • Best for: Internal tools, small teams, or prototypes with < 100 concurrent users.
      • Architecture: A Node.js server (e.g., Express or Hono) manages an AgentPool and uses a SqliteStore or PostgresStore for persistence.
    3. Pattern 3: Worker Microservice

      • Best for: Production SaaS with 1000+ concurrent users.
      • Architecture: Stateless API servers receive requests and enqueue tasks into a Job Queue (e.g., BullMQ). Dedicated Worker processes consume these jobs, manage an AgentPool, and use PostgresStore, Redis (for caching/PubSub), and S3 (for files). This pattern uses distributed locks via the store to manage agent concurrency.
    4. Pattern 4: Hybrid (Serverless + Workers)

      • Best for: Serverless frontends (Vercel/Cloudflare) paired with stateful backends.
      • Architecture: A serverless API validates requests and enqueues tasks (e.g., via Inngest or Upstash Redis). Long-running worker processes on platforms like Railway or Fly.io handle the actual KODE SDK execution.
  7. Handle breaking changes in KODE SDK

    main

    Breaking changes should be avoided. If they are unavoidable, you must follow these requirements:

    1. Mark the PR: Include BREAKING in the PR title or description.
    2. Submit a Report: Provide a detailed report including:
      • Scope of impact
      • Migration steps
      • Transition strategy (e.g., compatibility layers, deprecation periods)
      • Risks
      • Rollback plan
    3. Provide Solutions: Always attempt to provide transition solutions for users.
  8. Understand KODE SDK error types and retryability

    main

    KODE SDK categorizes errors into five types to help the model and the system decide how to react. Understanding these is critical for implementing smart retry logic or UI notifications.

    Error TypeIdentifierRetryableTypical Scenarios
    validation_validationError: trueNoParameter type error, missing required params
    runtime_thrownError: trueYesFile not found, permission denied, network error
    logicalTool returns {ok: false}YesContent mismatch, command execution failed
    abortedTimeout/interruptNoTool execution timeout, user interrupt
    exceptionUnexpected exceptionYesSystem exception, unknown error
  9. Define a skill using the SKILL.md format

    main

    Every skill requires a SKILL.md file in its directory. This file uses YAML frontmatter for metadata and Markdown for content. The name in the YAML must match the directory name.

    Required Fields:

    • name: Skill identifier (must match folder name).
    • description: Description used for automatic injection into the Agent's system prompt.

    Directory Structure:

    .skills/
    ├── skill-name/
    │   ├── SKILL.md            # Required definition
    │   ├── references/         # Reference documents
    │   ├── scripts/            # Executable scripts
    │   └── assets/             # Static resources
    └── .archived/
        └── archived-skill/
    ---
    name: skill-name
    description: Skill description
    ---
    
    # Skill Name
    
    Brief description of the skill's functionality.
    
    ## Use Cases
    
    - Case 1
    - Case 2
    
    ## Usage Guide
    
    Detailed instructions for using this skill...
  10. Performance and Scaling Considerations

    main

    When building with KODE SDK, keep the following runtime characteristics in mind:

    Memory Footprint

    A typical agent instance consumes between 100KB and 5MB, depending on message history and media cache. An AgentPool of 50 agents may require 5MB to 250MB of memory.

    I/O and Scaling

    • I/O Overhead: Each agent step incurs approximately 30-70ms of I/O overhead (persistence and event emission).
    • Concurrency: For high-concurrency environments (e.g., 100+ agents), avoid JSONStore due to sequential bottlenecks. Instead, use SqliteStore or PostgresStore to support parallel writes.
    • Event Loop: While most heavy operations are async, be cautious with synchronous operations in custom tools, as they can block the event loop.
  11. Implement a custom Store

    main

    In current versions (v2.7.0), the SDK uses JSONStore for file persistence. To support database persistence, you can implement the Store interface.

    Planned improvements in v2.8 will introduce appendMessage() for incremental updates and loadMessagesPaginated() for handling large histories. When implementing a custom store, ensure it satisfies the core Store interface requirements to allow for seamless integration with the Agent lifecycle.

    interface Store {
      // Existing methods...
    
      // NEW (v2.8): Incremental append (optional, for performance)
      appendMessage?(agentId: string, message: Message): Promise<void>;
    
      // NEW (v2.8): Paginated loading (optional, for large histories)
      loadMessagesPaginated?(agentId: string, opts: {
        offset: number;
        limit: number;
      }): Promise<Message[]>;
    }