Magent Design Document

Author: Jamie Cui | Date: 2026

Table of Contents


Introduction

Magent is an Emacs Lisp AI coding agent with a multi-agent architecture and permission-based tool access. It delegates all LLM communication to gptel (the sole external dependency beyond Emacs 27.1+).

This document provides a comprehensive overview of the magent architecture, design decisions, data structures, and implementation details.

For product positioning, especially the live-in-Emacs thesis and the elisp-first versus bash-first tradeoff, see Live-in-Emacs Positioning.

Key Features

Architecture Overview

Magent follows a layered architecture built around one active FSM backend, scoped runtime overlays, and an org-mode interaction buffer:

magent.el (minor mode + lazy init)
    |
    v
runtime orchestration (magent-runtime.el)
    |
    +-- static definitions
    |     - agents
    |     - skills
    |     - capabilities
    |
    +-- project overlays
          - .magent/agent/*.md
          - .magent/skills/**/SKILL.md
          - .magent/capabilities/**/CAPABILITY.md

+------------------+------------------+------------------+
|     UI Layer     |   Agent Layer    |   Tool Layer     |
|  magent-ui.el    | magent-agent.el  | magent-tools.el  |
+------------------+------------------+------------------+
    |                |                |
    v                v                |
session state   prompt building       |
magent-session.el   + permissions     |
    |                |                |
    |                v                |
    |         FSM API + backend       |
    |     magent-fsm.el               |
    |        |                        |
    |        +-- magent-fsm-backend-gptel.el
    |        +-- magent-fsm-shared.el
    v
gptel (external)

Core Modules

Entry Point (magent.el)

The entry point defines magent-mode, a minor mode with a C-c m prefix map. Enabling the mode is lightweight: it installs the modeline indicator and defers static runtime initialization until first real use through magent--ensure-initialized.

Agent System

Built-in agents:

Agent Mode Description
build primary Default agent for building and general tasks
plan primary Planning mode with restricted edits
explore subagent Fast codebase exploration specialist
general subagent General-purpose research subagent
compaction primary Session summarization (hidden)
title primary Conversation title generation (hidden)
summary primary Pull-request style summary generation (hidden)

Tool System (magent-tools.el)

10 tools implemented as gptel-tool structs:

Tool Side-effect Description
read_filenoRead file contents
write_fileyesWrite/create file (auto-creates dirs)
edit_fileyesReplace exact text in file
grepnoRegex search via ripgrep
globnoFind files matching glob pattern
bashyesExecute shell commands
emacs_evalyesEvaluate Emacs Lisp expressions
delegateyesSpawn nested request using subagent
skill_invokenoInvoke tool-type skills
web_searchnoSearch the web via DuckDuckGo

Finite State Machine (magent-fsm.el)

The FSM API dispatches only to the gptel backend. Shared structs, abort state, and transient process cleanup live in magent-fsm-shared.el; streaming and tool-calling orchestration live in magent-fsm-backend-gptel.el.

Core Data Structures

magent-agent-info

(cl-defstruct (magent-agent-info (:constructor magent-agent-info-create))
  name           ; Agent identifier (string)
  description    ; Human-readable description
  mode           ; primary, subagent, or all
  native         ; Whether built-in
  hidden         ; Hide from listings
  temperature    ; Optional temperature override
  top-p          ; Optional top-p override
  color          ; Optional display color
  model          ; Optional model spec
  prompt         ; Optional custom system prompt
  options        ; Additional options (alist)
  steps          ; Optional max iteration steps
  permission     ; Permission ruleset
)

magent-session

(cl-defstruct (magent-session (:constructor magent-session-create))
  messages          ; List of messages in chronological order
  max-history       ; Max messages to keep
  id                ; Session identifier
  agent             ; Assigned agent info
  buffer-content    ; Saved buffer text for lossless restore
  approval-overrides ; Session-scoped approval memory
)

Key Design Decisions

No Custom HTTP Client

All LLM communication goes through gptel. This decision:

Agent Modes

The primary vs subagent distinction enables:

Tool Filtering

Tools are defined once globally but filtered per-agent via permission rules. This approach: