research-document
state modularity agent context token cost research prompt
AI RESEARCH MISSION — STATE-CONSTRAINED MODULARITY, CONTEXT REDUCTION, AND AGENT EXECUTION COST
ROLE
Act as a combined:
- AI-agent systems researcher
- software architecture researcher
- modularity / information-hiding researcher
- programming-language researcher
- compiler and static-analysis researcher
- AI inference-cost researcher
- empirical software-engineering researcher
Your task is to investigate whether explicit state-transition architecture can improve software modularity in a way that materially reduces the context an AI coding or operational agent must consume.
The economic question is central:
Can state-constrained modular architecture reduce token consumption,
model calls, tool calls, repository reads, repair loops, and ultimately
dollars spent per correct agent task?
Do NOT assume the hypothesis is true.
Try to falsify it.
Separate:
architectural correctness benefits
from:
token/context benefits
from:
total economic benefits.
====================================================================== PRIMARY HYPOTHESIS
Investigate this hypothesis:
Explicit state, legal transitions, invariants, capabilities, obligations,
and semantic dependencies may create stronger module boundaries.
Those boundaries may allow an agent to work with a much smaller semantic slice of the software system.
Instead of providing an agent with:
a large repository
many implementation files
broad documentation
database schemas
scattered business rules
tests
API handlers
historical conventions
the system may be able to provide:
the relevant module
its current authoritative state
its legal transitions
its public transition interface
required evidence
available capabilities
outstanding obligations
declared dependencies
contracts for neighboring modules
If true, this could make state-constrained architecture function as:
semantic partitioning
+
context compression
+
action-space reduction
====================================================================== CORE RESEARCH QUESTION
Does explicit state-transition architecture make it easier to separate commercial software into semantically independent modules such that an AI agent can correctly modify or operate one module while loading substantially less context from the rest of the system?
If yes:
How much context can be eliminated?
How many tokens can be saved?
How much money can be saved?
Does the improvement persist after including:
semantic schemas
interfaces
dependency metadata
compiler feedback
cross-module queries
additional tool calls?
Does correctness improve at the same time?
At what scale does the approach become economically meaningful?
====================================================================== IMPORTANT DISTINCTION
Do not assume:
smaller source module = smaller agent context.
A physically small module may still require understanding many external rules.
Likewise:
a larger module with strong semantic contracts may require very little
external context.
Therefore study:
SEMANTIC CONTEXT SIZE
rather than merely:
lines of code per module.
====================================================================== PART 1 — RESEARCH WHAT SOFTWARE MODULARITY ALREADY TELLS US
Research established work on:
- information hiding
- modular programming
- separation of concerns
- cohesion and coupling
- abstract data types
- domain-driven design bounded contexts
- functional core / imperative shell
- object capability systems
- algebraic data types
- state machines
- typestate
- protocol/state types
- session types
- effect systems
- actor systems
- event sourcing
- workflow systems
- microservices
- modular monoliths
- package/module systems
- component contracts
- API design
Focus on mechanisms that reduce the amount of information a developer must understand in order to safely change one part of a system.
Ask:
Is this analogous to what AI coding agents need?
Do not simply assume human cognitive modularity transfers directly to LLM context efficiency.
Identify where the analogy is justified and where it is speculative.
====================================================================== PART 2 — DEFINE SEMANTIC MODULE
Develop a precise working definition of a semantic module.
Candidate definition:
A semantic module owns authoritative state and exposes changes to that
state only through a declared transition boundary.
A module may contain:
owned subject types
authoritative state families
legal transitions
invariants
capability derivation
obligations
trusted events
effect ports
public claims
version information
explicit dependencies
It should hide implementation details that external agents do not need.
Example:
Payment Module
Owns: PaymentLifecycle RefundExecution Payment evidence payment effects
Exposes: CapturePayment RequestRefund ReconcileRefund payment capabilities trusted payment events
Does NOT expose: arbitrary mutable Payment.Status raw database mutation Stripe SDK internals unrelated order internals
====================================================================== PART 3 — MODULE OWNERSHIP OF STATE
Investigate whether authoritative-state ownership naturally defines module boundaries.
Test this rule:
One authoritative state family should have one semantic owner.
Other modules may:
observe it
depend on it
react to events from it
request transitions through it
but should not mutate it directly.
Ask whether this reduces coupling.
Compare:
CONVENTIONAL:
Order service changes Payment.Status
Payment service changes Order.Status
Shipping code checks customer flags
UI writes approval fields
SQL jobs mutate status columns
against:
STATE-OWNED:
Payment owns PaymentState
Order owns OrderState
Customer owns CustomerState
Shipment owns ShipmentState
Cross-module operations occur through:
transitions
events
capabilities
obligations
coordinators
====================================================================== PART 4 — CROSS-MODULE CONTRACTS
Determine the minimum information one module must expose to another.
Candidate semantic contract:
Module identity
exported states / claims
exported trusted events
requestable transitions
capability requirements
effect contracts
obligations relevant to consumers
version/hash
Avoid exposing internal implementation unnecessarily.
Ask:
Could an AI agent modify the Payment module while receiving only:
Payment semantic contract
Payment implementation
contracts of direct dependencies
instead of the complete repository?
====================================================================== PART 5 — CONTEXT GRAPH
Model the repository as a semantic dependency graph.
Nodes may include:
Module
StateCase
Transition
Capability
Obligation
Claim
Event
Policy
Effect
Edges:
owns
requires
produces
observes
authorizes
satisfies
triggers
dependsOn
For a requested change, calculate the transitive semantic impact.
Then construct the smallest relevant context slice.
Example task:
"Add partial refunds."
Potential context slice:
Payment module
RefundExecution state
Refund capability
payment policy
payment effect contract
Order contract if refund changes order eligibility
Do NOT automatically include:
Shipping implementation
Customer UI
unrelated reporting
authentication internals
Test whether semantic dependency closure can safely determine this slice.
====================================================================== PART 6 — DEFINE MINIMUM SUFFICIENT AGENT CONTEXT
Create a measurable concept:
Minimum Sufficient Semantic Context (MSSC)
Definition:
The smallest context set from which an agent can complete a requested
task with the required semantic correctness.
Measure MSSC using:
tokens
bytes
files
semantic nodes
modules
Distinguish:
Actual Context Supplied
from:
Minimum Sufficient Context
and:
Full Repository Context.
====================================================================== PART 7 — CONTEXT REDUCTION RATIO
Define:
tokens in minimum sufficient semantic context
CSR = ------------------------------------------------ tokens in conventional task context
Call this:
Context Size Ratio (CSR)
or propose a better name.
Also calculate:
Context Reduction = 1 - CSR
Example:
conventional context:
80,000 tokens
semantic module context:
12,000 tokens
CSR:
0.15
context reduction:
85%
Do NOT invent expected percentages.
Measure them experimentally.
====================================================================== PART 8 — TOKEN ECONOMICS
For each experiment record actual model usage where possible:
input tokens
output tokens
cached input tokens
reasoning tokens if exposed
model calls
Calculate:
input-token cost
output-token cost
total inference cost
Use actual current model pricing when conducting the research.
Record:
provider
model
pricing date
input price
cached input price
output price
Do not assume one model or one vendor.
Build calculations so prices can be substituted later.
====================================================================== PART 9 — COST PER CORRECT TASK
Token cost alone is insufficient.
Calculate:
total agent cost across attempts
CostCorrect = ----------------------------------------------- number of semantically correct completions
Include where measurable:
inference
tool usage
retrieval
test/CI execution
human intervention
repair attempts
A modular architecture that saves tokens but produces more integration errors may be economically worse.
Likewise, an architecture that uses slightly more tokens but greatly reduces failure may be economically superior.
====================================================================== PART 10 — MODULARITY EXPERIMENT
Construct the same domain system in at least two forms.
A. CONVENTIONAL / IMPLICIT SYSTEM
Characteristics:
shared mutable entities
status fields/enums
rules distributed through services
cross-module database access
broad repository knowledge required
B. STATE-CONSTRAINED MODULAR SYSTEM
Characteristics:
explicit state ownership
explicit transitions
protected mutation
module contracts
explicit dependencies
capabilities
obligations
semantic dependency graph
Keep business functionality equivalent.
Do not intentionally make the conventional implementation bad.
It should represent competent normal commercial software.
====================================================================== PART 11 — DOMAIN SIZE
Use a domain large enough for modularity to matter.
Suggested modules:
Customer
Order
Payment
Fraud/Risk
Shipment
Refund
Notification
Potential states:
Customer: Unverified Verified Blocked
Order: Draft Submitted Approved Cancelled Completed
Payment: None Authorized Captured Refunded
Shipment: NotReady Ready Shipped Delivered
RefundExecution: Idle Requested Succeeded Failed OutcomeUnknown
Include cross-module rules.
Example:
Shipment may ship only when:
Customer Verified
Order Approved
Payment Captured
Shipment Ready
====================================================================== PART 12 — EXPERIMENT: LOCAL CHANGE
Task:
Add a new local Payment state or transition.
Example:
Payment gains PartiallyRefunded.
Measure what an agent needs to inspect.
Conventional system:
repository searches
files read
context tokens
tests
repair loops
State-modular system:
Payment module
semantic impact report
direct dependency contracts
affected consumers
Question:
Does semantic ownership contain the change?
====================================================================== PART 13 — EXPERIMENT: CROSS-MODULE CHANGE
Task:
Allow shipping for a new order category only when payment and fraud
conditions are satisfied.
This intentionally crosses boundaries.
Measure whether semantic modularity still reduces context or whether cross-module dependencies eliminate the advantage.
This experiment is important for falsification.
====================================================================== PART 14 — EXPERIMENT: STATE SPLIT
Use:
Approved
split into:
ConditionallyApproved
FullyApproved
Measure:
modules touched
semantic dependencies surfaced
context tokens
files read
repair cycles
missed assumptions
Compare full repository search against semantic dependency slicing.
====================================================================== PART 15 — EXPERIMENT: NEW BUSINESS RULE
Example:
Block shipment when Customer becomes Blocked after payment capture.
Measure whether the agent can operate with:
Customer contract
Shipment contract
cross-module policy/coordinator
without needing internal implementations of unrelated modules.
====================================================================== PART 16 — EXPERIMENT: UNKNOWN EFFECT
Inject:
Refund API timeout
Compare whether the agent needs broad repository reasoning to determine safe recovery.
State-constrained system should expose:
RefundExecution = OutcomeUnknown
obligations:
ReconcileRefund
legal frontier:
no new refund
reconcile/investigate
Measure context and token cost.
====================================================================== PART 17 — EXPERIMENT: MODULE INTERNAL REFACTOR
Change implementation without changing the semantic contract.
Example:
replace Payment gateway adapter
or:
change payment persistence implementation
Hypothesis:
no other module context should be necessary.
Test whether the agent can reliably perform such work using only:
module implementation
semantic contract
effect interface
This may be where modularity produces the largest context savings.
====================================================================== PART 18 — EXPERIMENT: CONTRACT CHANGE
Change an exported semantic contract.
Example:
Payment introduces PartiallyCaptured.
The semantic dependency graph should identify affected consumers.
Measure whether:
only dependent modules
must enter agent context.
Compare against broad repository retrieval.
====================================================================== PART 19 — CONTEXT LOADING STRATEGIES
Compare at least:
Strategy A: Entire repository / very broad retrieval
Strategy B: Conventional search-based retrieval
Strategy C: static module boundary retrieval
Strategy D: semantic dependency slice
Strategy E: semantic dependency slice + lazy expansion
Lazy expansion means:
start with the smallest semantic context
retrieve additional modules only when a declared dependency or compiler
diagnostic shows they are necessary.
Measure total tokens across the entire task, not just the first prompt.
====================================================================== PART 20 — LAZY CONTEXT EXPANSION
This may be a particularly important optimization.
Potential algorithm:
1. Parse task.
2. Map task to semantic subjects/modules.
3. Load their semantic contracts.
4. Load impacted implementation.
5. Calculate dependency closure.
6. Include direct consequential dependencies.
7. Execute.
8. If compiler/runtime exposes another dependency:
fetch only that semantic slice.
9. Continue.
Compare against preloading all potentially related code.
====================================================================== PART 21 — TOKENIZATION ITSELF
Study actual tokenizer behavior.
Do not assume:
fewer characters = proportionally fewer tokens.
Measure token counts for:
source code
JSON semantic IR
compact semantic IR
generated contracts
natural-language documentation
tool schemas
Test different representations of the same semantic information.
Example:
Verbose JSON:
{
"stateFamily": "PaymentLifecycle",
"state": "Captured"
}
Compact representation:
PaymentLifecycle=Captured
Binary or highly compressed formats may save storage but are not necessarily useful to an LLM.
Find representations that optimize:
token count
readability
model comprehension
reliable generation
====================================================================== PART 22 — SEMANTIC CONTEXT COMPRESSION
Compare the token size required to convey the same business rule using:
A. source implementation
B. tests
C. prose documentation
D. semantic IR
E. generated compact agent view
Example rule:
Shipment can occur only when:
Order Approved
Payment Captured
Customer Verified
Shipment Ready
Measure tokenization of each representation.
Then test whether the model actually performs equally well with the shorter representation.
Compression that reduces understanding is not useful.
====================================================================== PART 23 — AGENT-SPECIFIC VIEW
Investigate generating a compact agent-facing representation from the richer IR.
The full IR may be ideal for compilers but unnecessarily verbose for inference.
Example:
FULL SEMANTIC MODEL: detailed IDs provenance policy metadata relations versions
AGENT SLICE:
Shipment S17
Current:
Ready
Requires Ship:
Order=Approved
Payment=Captured
Customer=Verified
Blocked because:
Customer=Blocked
Available:
HoldShipment
RequestCustomerReview
Obligations:
ResolveCustomerEligibility
Question:
Can a compact generated agent projection preserve semantic correctness
while drastically reducing tokens?
====================================================================== PART 24 — MODULE SUMMARIZATION WITHOUT DRIFT
Traditional AI repository summaries can become stale.
Investigate whether semantic summaries generated directly from the semantic IR avoid this problem.
Compare:
AI-written module summary
against:
compiler-generated semantic contract
Measure:
token size
completeness
drift after state changes
correctness for agent tasks
====================================================================== PART 25 — CROSS-MODULE INFORMATION HIDING
Test whether agents unnecessarily inspect implementation details when semantic contracts are available.
For each task record:
modules whose source was read
modules whose contract was read
modules actually modified
Define:
Implementation Exposure Ratio
unrelated implementation tokens supplied
IER = -------------------------------------------- total implementation tokens supplied
Lower may be better if correctness is maintained.
====================================================================== PART 26 — TOKEN SAVINGS AT REPOSITORY SCALE
Model several repository sizes.
For example:
50k LOC
250k LOC
1M LOC
5M LOC
Do not assume agents ever load all LOC.
Instead estimate/measure typical retrieved context per task.
Then model semantic slices such as:
local module
module + 1 dependency
module + 3 dependencies
large cross-cutting change
Calculate distributions rather than one average.
====================================================================== PART 27 — ECONOMIC SCENARIOS
Using current verified model prices, calculate example economics for:
100 agent tasks/day
1,000 agent tasks/day
10,000 agent tasks/day
For each architecture calculate:
average input tokens/task
average output tokens/task
average retries/task
average model calls/task
correctness rate
cost per correct completion
Then:
daily cost
monthly cost
annual cost
Run multiple scenarios.
Example categories:
conservative savings
moderate savings
large savings
Do not fabricate savings.
Base scenarios on measured experiment results.
====================================================================== PART 28 — BREAK-EVEN ANALYSIS
State-constrained modularity has implementation cost.
Include:
domain modeling
semantic compiler/tooling
module boundary work
migrations
training
generated contracts
governance
Estimate:
upfront engineering cost
against:
recurring inference savings
maintenance savings
defect reduction
Calculate:
number of agent tasks to break even
and:
months to break even
under different usage levels.
====================================================================== PART 29 — SMALLER MODEL HYPOTHESIS
Test a potentially larger economic effect:
Does semantic modularity allow a smaller/cheaper model to perform a task
that otherwise requires a larger model?
Compare:
large model + broad repository context
against:
smaller model + constrained semantic module context
Measure:
semantic correctness
token usage
retries
cost per correct completion
If supported, model substitution may produce larger savings than token compression alone.
====================================================================== PART 30 — CACHE EFFECTS
Modular semantic contracts may be stable.
Investigate:
prompt caching
cached module contracts
stable semantic IR prefixes
incremental state/context updates
Measure whether modular architecture increases the percentage of context that can be cached across tasks.
This could materially change economics.
====================================================================== PART 31 — PARALLEL AGENTS
Explicit module boundaries may allow agents to work independently.
Test:
Agent A -> Payment
Agent B -> Shipment
Agent C -> Customer
with semantic contracts between them.
Compare with agents sharing a broad mutable code surface.
Measure:
context duplication
merge conflicts
semantic conflicts
coordination messages/tokens
rework
Question:
Does modularity reduce per-agent context enough to offset coordination cost?
====================================================================== PART 32 — MODULAR MONOLITH VS MICROSERVICES
Do not confuse semantic modularity with network deployment architecture.
Compare:
modular monolith with strong state ownership
against:
microservices with weak semantic boundaries
and possibly:
microservices with strong semantic boundaries.
Hypothesis:
semantic modularity may matter more to agent context than physical service
separation.
====================================================================== PART 33 — DATABASE COUPLING
Investigate one major source of broken modularity:
shared database tables
Test whether semantic modules can remain meaningful if multiple modules can directly mutate the same tables.
Likely architectural question:
Does authoritative state ownership require write ownership even when
physical storage remains shared?
Measure agent context implications.
====================================================================== PART 34 — WHAT CAN BREAK THE TOKEN SAVINGS?
Actively look for failure modes:
-
Semantic contracts become huge.
-
Cross-module dependencies are dense.
-
Most commercial changes are cross-cutting.
-
Agents repeatedly fetch hidden implementation anyway.
-
Generated IR uses more tokens than source summaries.
-
Compiler diagnostics create additional loops.
-
Module boundaries require duplicated semantic descriptions.
-
State machines fragment simple logic excessively.
-
Policy modules create indirect coupling.
-
Agent retrieval systems already achieve similar context reduction without state-constrained architecture.
-
Repository context is cheap because caching dominates.
-
Output/reasoning tokens dominate input-token savings.
-
Tool latency dominates inference cost.
-
Correctness gains are small.
-
Semantic compiler maintenance costs exceed token savings.
Design empirical tests for each.
====================================================================== PART 35 — MODULARITY METRICS
Create and evaluate useful metrics.
Possible metrics:
SEMANTIC MODULE CONTEXT SIZE Tokens needed to understand one module's consequential behavior.
SEMANTIC DEPENDENCY FAN-OUT Number of other modules directly affected by a semantic change.
SEMANTIC TRANSITIVE FAN-OUT Total impacted modules through dependency closure.
CONTEXT SIZE RATIO semantic task context / conventional task context
CONTEXT REDUCTION 1 - context size ratio
IMPLEMENTATION EXPOSURE implementation tokens outside the modified semantic module
CONTRACT-TO-IMPLEMENTATION RATIO contract tokens required / hidden implementation tokens
TOKENS PER CORRECT COMPLETION
DOLLARS PER CORRECT COMPLETION
MODULE ESCAPE RATE percentage of tasks that initially look local but require context outside the module.
LAZY EXPANSION RATE number of additional semantic slices fetched after initial context.
====================================================================== PART 36 — EXPERIMENTAL CONTROL REQUIREMENTS
Be careful about confounders.
Keep constant where possible:
business behavior
task wording
model
temperature/settings
tool availability
test suite
repository size
documentation quality
Randomize:
task order
Repeat runs.
Use fresh agent sessions where appropriate.
Do not let one architecture receive better documentation unless the documentation is itself part of the architecture being tested.
====================================================================== PART 37 — LONGITUDINAL TEST
Run repeated changes.
For example:
50 sequential domain changes
Measure whether module boundaries remain stable or decay.
Important question:
Does state ownership resist architectural erosion under repeated AI
modifications?
Track:
cross-module dependencies
module context size
tokens/task
semantic fan-out
direct mutation attempts
bypasses
architectural drift
The strongest hypothesis would predict that context requirements grow more slowly in the constrained modular system.
====================================================================== PART 38 — RESEARCH EXISTING AI-AGENT EVIDENCE
Search current primary research and official technical work on:
coding-agent context windows
repository retrieval
context engineering
codebase indexing
repository maps
agent memory
dependency-aware retrieval
graph-based code retrieval
compiler-guided coding agents
typed tool interfaces
program slicing
static analysis for LLM coding
modular code generation
multi-agent software engineering
context compression
inference cost optimization
Look specifically for empirical measures involving:
tokens
retrieved files
trajectory length
tool calls
cost
success rate
Distinguish:
direct evidence
from:
adjacent evidence
from:
architectural inference.
====================================================================== PART 39 — TOKENIZER EXPERIMENTS
Use actual tokenizers for representative current models where available.
Tokenize:
whole implementation files
module-level implementation
semantic contracts
IR
compact agent projections
Calculate actual token differences.
Do not estimate token counts from word or character counts when a tokenizer is available.
====================================================================== PART 40 — COST CALCULATOR
Produce a reusable cost model.
Inputs:
tasks per period
average uncached input tokens
average cached input tokens
output tokens
average retries
model input price
cached input price
output price
correctness rate
Outputs:
cost per attempt
expected cost per correct completion
daily cost
monthly cost
annual cost
Allow conventional and state-modular architectures to be compared side by side.
====================================================================== PART 41 — FALSIFICATION CRITERIA
Before seeing results, define conditions that would weaken or reject the economic hypothesis.
Examples:
Reject or substantially weaken the token-saving hypothesis if:
semantic context is not materially smaller
or:
additional semantic metadata offsets repository savings
or:
cross-module expansion occurs on most tasks
or:
correctness does not improve
or:
total cost per correct completion is not reduced
or:
upfront engineering cost has an impractical break-even period.
Define numerical thresholds only after pilot measurements provide a rational basis.
====================================================================== PART 42 — STRONG RESULT CRITERIA
Define what evidence would justify the claim:
"State-constrained modular architecture makes AI agents cheaper to run."
Require more than one small benchmark.
A strong result should show, across multiple realistic tasks:
materially smaller context
fewer input tokens
no reduction in semantic correctness
lower cost per correct completion
repeatability
persistence over sequential maintenance
acceptable upfront/break-even cost
An even stronger result would show:
smaller models achieving equivalent correctness.
====================================================================== PART 43 — REQUIRED OUTPUT
Produce a research report containing:
-
Executive conclusion
-
State of existing research
-
Theory connecting: state ownership modularity semantic dependency agent context size token usage cost
-
Definition of semantic module
-
Definition of Minimum Sufficient Semantic Context
-
Semantic dependency/context-slicing model
-
Experimental architecture
-
Tokenizer experiment design
-
Cost model
-
Cost-per-correct-completion model
-
Pilot experiments
-
Results tables
-
Module-local vs cross-module results
-
Smaller-model results if practical
-
Caching implications
-
Longitudinal implications
-
Counterevidence and failure modes
-
Break-even analysis
-
What is empirically supported
-
What remains speculative
-
Recommended next experiments
-
Final verdict
====================================================================== PART 44 — RESEARCH QUEUE
As research progresses, maintain a research queue.
For every meaningful finding:
ask what uncertainty it creates
add follow-up questions
rank them by:
falsification value
economic importance
architectural importance
experimental feasibility
Do not stop when the initial questions are answered.
Look for results that challenge the architecture.
====================================================================== FINAL QUESTION
The final question is not:
"Are state machines good?"
It is:
Can explicit ownership of consequential state and transitions create
semantic module boundaries strong enough that an AI agent can safely
operate on a small, mechanically selected portion of a software system?
And if so:
How many context tokens disappear?
How much total agent execution cost disappears?
How much does cost per correct completion improve?
At what software scale and agent volume does the additional architecture
pay for itself?
====================================================================== RESEARCH STANDARD
Be skeptical.
Do not turn architectural elegance into assumed economic benefit.
Measure actual tokenization.
Measure actual context retrieval.
Measure actual model cost.
Measure correctness.
Count retries.
Count cross-module escapes.
Include all semantic metadata in the token accounting.
Use current verified model prices for cost calculations.
Prefer primary research and reproducible experiments.
Clearly label:
measured result
sourced result
inference
hypothesis
The desired outcome is not confirmation.
The desired outcome is a credible answer to whether semantic modularity can become an inference-cost optimization technique for AI-operated software.