# Code as a Contract > Business-logic code is a contract between its parties (people and system actors like the database, queue or an LLM) that a machine happens to run. Name every rule as a clause, keep terms pure, perform at the edges, and write tests as precedents (in legal terms: case law). ## How to implement (agents) 1. List every party (people and systems) and its duties before you write types or functions. (https://code-as-contract.org/pattern/#principle-1) 2. Write each business rule as a named pure function in its party's module (contract/.py). Name the numbers as constants. (https://code-as-contract.org/pattern/#principle-2) 3. Write the main function as a straight sequence of those calls. (https://code-as-contract.org/pattern/#principle-3) 4. Do side effects only after the decision, and use only the decision's fields. (https://code-as-contract.org/pattern/#principle-4) 5. Put retries, caching, limits and permissions in frozen policy data. (https://code-as-contract.org/pattern/#principle-9) 6. Write precedents: one pure test per rule in precedents/test_.py. No mocks. Name each as a plain sentence of expected behavior. (https://code-as-contract.org/pattern/#principle-5) 7. Enforce the structure with tests too: every party module has a clause, and every clause is called by a precedent. (https://code-as-contract.org/pattern/#principle-10) ## Pattern - [Lean (Markdown)](https://code-as-contract.org/lean/code-as-contract-lean.md): the pattern in about 800 tokens. Read this first. - [Pattern (Markdown)](https://code-as-contract.org/pattern/CODE_AS_A_CONTRACT.md): the full text, raw Markdown. - [Pattern (HTML)](https://code-as-contract.org/pattern/): the same text, rendered. ## Agent skill - [SKILL.md](https://github.com/intrepid-g/code-as-contract/blob/main/skill/SKILL.md): the code-as-contract skill for coding agents. - [Repository](https://github.com/intrepid-g/code-as-contract): source, versions and changelog. ## Optional - [Schedules](https://code-as-contract.org/schedules/): how caching, queues, permissions and other internals are drafted. - [Code as Contract vs. Gherkin](https://code-as-contract.org/comparisons/gherkin/): a comparison paper.