Leanpub Header

Skip to main content

Double-Entry Accounting - A Literate Programming Implementation

C, SQLite and an AI bookkeeper, built chunk by chunk

Double-Entry Accounting - A Literate Programming Implementation
This book is 90% completeLast updated on 2026-10-23

A book that compiles into the software it describes.

Learn double-entry accounting by building a complete ledger in C and SQLite, using literate programming. From journal entries and accounting invariants to an AI-powered bookkeeper through MCP, every component is explained and implemented.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

Double-entry bookkeeping has kept the world's books for more than five centuries. This book explains it from first principles, and builds it in C as you read.

The book is a literate program: the code you read on the page is the code that gets compiled. Chapter by chapter it builds `libledger`, a small C library that keeps double-entry books in a single SQLite file. On top of the library it builds the `ledger` command and an MCP server through which an AI assistant can keep the books, but only with your approval.

You need to know some C. You don't need to know any accounting.

You will follow Alex, a freelance graphic designer, through her first week of books: invoicing a client, paying rent, fixing a mistake, getting paid, closing the period. Later you will live one of her days twice, once by hand and once by talking to an assistant, and see the balances come out the same.

Along the way you will learn:

- Accounting: debits and credits, the accounting equation, a US-style chart of accounts (49 accounts), accrual vs. cash, the trial balance and closing entries.

- Data modeling: an 8-table SQLite schema, an account tree derived from dotted codes, and cached balances that always agree with their history.

- Money done right: amounts are 64-bit integers in the smallest unit, with overflow checks. Floating point is never used for money. Every currency balances on its own, and values are converted to USD (mark to market) only when the books are read.

- Correctness: every entry is validated and written inside one `BEGIN IMMEDIATE` transaction. Negative balances are rejected, and four consistency checks catch books damaged by hand.

- Tooling: a single-binary CLI, JSON input and output, and an MCP (Model Context Protocol) server with a ready-to-install skill for the assistant.

The stories in the book are executable chapters. The build extracts their shell sessions, runs them, and compares the output character by character with what the book prints, so the text cannot drift away from the program.

About 3,000 lines of C, with no dependencies beyond SQLite and cJSON. Each technical term, accounting or programming, is defined in a glossary at the end of the book.

A book that tests itself

One distinctive feature of this project is that the book and the software share the same source.

Using noweb, the implementation is extracted from the manuscript and compiled into working programs. Selected chapters go further: their command-line sessions are executed automatically, and their output is compared against the results printed in the book.

When the implementation changes, the documentation can be verified against it.

Who is this book for?

This book is intended for programmers who want to understand how financial software works, developers interested in literate programming, and engineers who appreciate small, understandable systems built from first principles.

Readers should have some familiarity with C. No previous accounting knowledge is required.

The result is both an introduction to double-entry bookkeeping and a detailed exploration of how to turn accounting principles into executable, verifiable software.

All the code is on GitHub: https://github.com/libledger/libledger

Share this book

Author

About the Author

Alex Insulae Rivers

I'm a programmer.

Contents

Table of Contents

Table of Contents

Preface Part I — Fundamentals
  1. Introduction
    • A Brief History of Double-Entry Bookkeeping
    • Double-Entry Bookkeeping
    • The Chart of Accounts
    • Journal Entries in Practice
    • Literate Programming
  2. A Week in Alex's Books
    • Opening the Books
    • Invoicing and Expenses
    • Getting Paid
    • The Trial Balance
    • Closing the Books
    • The Executable Script
Part II — Implementation
  1. Inside the Library
  2. Database Schema
  3. Lifecycle and Transactions
  4. Money and Currencies
  5. Chart of Accounts
  6. Journal Entries
  7. Consistency Checks
  8. Reading the Books Back
Part III — Programs
  1. The ledger Command
  2. Reporting Problems: ledger check
  3. Valuing the Books in USD: ledger mtm
  4. An AI Bookkeeper: the mcp Subcommand
    • Why Give an AI the Books?
    • Design Decisions
    • The Model Context Protocol
    • Reading and Writing the Books
    • Handling Requests
    • Connecting an AI Client
Part IV — In Practice
  1. One Day, Two Ways
    • The Day by Hand
    • The Day with an Assistant
    • Same Day, Same Balances
    • What Changed, and What Did Not
Appendices
  • Appendix A — A Skill for the AI Bookkeeper
  • Appendix B — Glossary
  • Index of Code Chunks

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub