Back to patterns

Module Contract

Responsibility & OwnershipOCLS CONTRACT

Declare the conditions, authority, and failure paths of every execution unit.


Context

As the modules an agent uses multiply, what each module accepts, what it returns, and when it fails becomes tacit knowledge. Replacing a module or reusing it from another agent then triggers errors no one predicted.

Problem

Without a documented interface you have no basis for evaluation and no compatibility guarantee on replacement. An unclear authority scope lets a module produce side effects beyond what was intended; an undefined failure condition means the agent has no way to detect or recover from failure.

Forces

  • A detailed contract is safer but constrains module flexibility.
  • Defining the contract first lets you validate design before implementation, but early requirements are unstable.
  • Runtime validation increases safety at the cost of performance.

Solution

Give every module an explicit contract: input schema, output schema, required authority, failure conditions, and the shape of the failure response. The contract must live in both code and documentation, and contract violations must be detectable at runtime. When adding or replacing a module, validate contract compatibility first. Anthropic's [Building Effective Agents] calls this interface the Agent-Computer Interface (ACI) and argues that we should invest in agent interface design the way we invest in human interface design — applying Poka-yoke (constraints that make misuse hard) to structurally prevent agent misuse. Contracts are not static documents. As in the Generator-Evaluator negotiation model from Anthropic's [Harness Design for Long-Running Application Development], contracts between agents can be negotiated and updated dynamically.

Judgment question

Under what conditions must this module refuse or fail?

Application scenario

Illustrative scenario — figures and company names in this page are hypothetical for explaining the pattern, not measured data.

A response-generation module used by the Response Agent. Contract: input is { category: string, context: string, tone: 'formal' | 'casual', maxTokens: number }; output is { response: string, confidence: number, sources: string[] }. Required authority: read access to the knowledge base. Refusal condition: if category falls under legal advice, refuse and return an escalation code. With this contract, swapping in a different LLM-backed module only requires confirming the new module honors the same schemas and refusal condition.

How it breaks

Running modules without contracts produces failures like "we changed the LLM and the response format shifted, so the QA Agent can't parse it" or "the new module answers legal-advice questions, so compliance just opened a ticket." Without defined failure conditions, agents treat bad output as good and proceed downstream.

Implementation pattern bridge

  • Spec-Driven Development

The contract's input/output schema becomes an executable spec that generates code, docs, and mocks. An MCP Tool Definition is one concrete realization of a module contract.

Academic References

  • ISO/IEC 25010 — Systems and Software Quality Models
  • Practices for Governing Agentic AI Systems — OpenAI

Apply this pattern

Use the review skill in your repository, then compare the implementation decisions in the related articles. Check each example against your own permissions, data, and failure modes.

  • Review skill: reopt-module-contractUse this Claude Code skill when adding or changing a module. Review input/output contracts, refusal conditions, and failure handling with a code example.

Related implementation notes

  • The development environment changes before the codeWhen agents started servers across projects and branches, ports and traffic became hard to follow. This case study covers building dev-proxy, an open source local proxy that combines subdomain routing, a terminal traffic inspector, and peers across machines, along with the problems that came up while rebuilding the environment for multi-project development.
  • Projects as design experiments: learning architecture with AIAcross 25 pilot projects, comparing projects with different development principles proved especially useful. This guide turns that experience into cross-project design review: make policies explicit, derive improvement hypotheses from reference projects, and verify them in the receiving project.
  • The problem is solved. Why isn't it a product?I built, with coding agents, what an observability SaaS used to do for us. Collecting logs, analysing them, and improving the code from them all worked through AI, and I got exactly the interface I wanted. It still was not a product. Put the minimum conditions for solving a problem and for being a product side by side on the four-layer stack, and they part ways at the point where the person who uses a tool, the person who runs it, and the person accountable for it stop being the same person. Whether to build or buy then stops being a question of replacement and becomes a choice, stage by stage, of how much you are willing to be accountable for.

Related patterns