Skip to main content

Context Compiler

PyPI version Python versions License codecov

Context Compiler helps LLM applications keep explicit premise and policy rules stable across turns. It blocks invalid or conflicting changes and returns structured decisions.

Use it when saved context and policy rules need to shape what an application does, not just what the model sees or says.

The context-compiler-example-integrations show this approach working in applications, and the demo results show consistent behavior across models and prompt conditions.

Quickstart

This example shows premise and policy updates directly:

from context_compiler import Engine, SemanticErrorDecision, UpdateDecision

engine = Engine()

engine.step("set premise project deadline is Friday")
print(engine.premise)
# project deadline is Friday

engine.step("change premise to project deadline is Thursday")
print(engine.premise)
# project deadline is Thursday

engine.step("use docker")

result = engine.step("use podman instead of docker")
if isinstance(result, UpdateDecision):
    print(result.changed)
# True

result = engine.step("use podman")
if isinstance(result, UpdateDecision):
    print(result.changed)
# False

result = engine.step("use npm instead of yarn")
if isinstance(result, SemanticErrorDecision):
    print(result.message)

# "yarn" is not currently in use.
# Replacement requires an active 'use' policy.

Installation

Requirements:

  • Python 3.11+

Install:

pip install context-compiler

Packaging notes:

  • Base install includes the Context Compiler engine and CLI.
  • Example and demo source files are available in the repository and source distribution.
  • To run the demos from this repository, clone the repo and install context-compiler[demos].
  • The [demos] extra installs optional dependencies such as LiteLLM. It does not install demo source files into site-packages.

API and behavior

The main public API is Engine and its Decision results.

  • Engine.step(...) accepts raw input and may return no_directive, update, or error.
  • Engine.apply_directive(...) accepts a supported directive and returns an update or error.
  • engine.premise and engine.policies expose live compiler state.
  • engine.export_json() and engine.import_json(...) transport compiler state only. See the API reference for fields, signatures, and repair behavior.

State Model

The engine stores explicit user commitments as saved state:

State Meaning
premise One factual or contextual value that changes how future answers should be interpreted
use An affirmative per-item policy
prohibit An excluding per-item policy

Directive commands

Set or change the premise with:

set premise project deadline is Friday
change premise to project deadline is Thursday

Policy commands:

use docker
prohibit peanuts

To replace an existing use policy:

use podman instead of docker

If docker is absent from saved state, the replacement fails and leaves state unchanged. It does not become plain use podman.

To remove a policy or clear state:

remove policy peanuts
reset policies
clear state

When to use premise

Use premise for persistent background context or factual state that changes how answers should be interpreted, especially when it:

  • applies across many turns
  • significantly changes what solutions are valid
  • cannot be fully captured as simple use / prohibit policies

Examples:

  • set premise current medications: …
  • set premise outdoor event; no seating available
  • set premise GDPR data handling requirements apply
  • set premise system is deployed across multiple regions
  • set premise project deadline is Friday

The premise stays in effect until it is changed or cleared.

Use policies instead when the constraint is explicit and enforceable.

use concise replies
use a formal tone
prohibit introducing new external dependencies

Examples and demos


CLI and REPL

The package includes an interactive REPL and a machine-readable JSON CLI. See the CLI and REPL guide for commands, preload options, and JSON output behavior.


Documentation

For the full map, see docs/README.md.


License

Apache-2.0.

Release files for context-compiler 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for context-compiler 0.9.0
File Size Uploaded
context_compiler-0.9.0.tar.gz 50.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for context-compiler 0.9.0
File Interpreter ABI Platform
context_compiler-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 72.4 kB

Release files / context_compiler-0.9.0.tar.gz

Download URL context_compiler-0.9.0.tar.gz
Size 50.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ae67f46e18501ea3dc718b4b24ba0ff7e88ee8198172d817effd88511ea36674
BLAKE2b-256 checksum
How to use checksums
6a4284146874200d823de12470e9a9bac93c0ebfe81566a020b10dde416b6e86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

Release files / context_compiler-0.9.0-py3-none-any.whl

Download URL context_compiler-0.9.0-py3-none-any.whl
Size 22.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1081cf95b22cc63f3447e5d49bc6233e7542d6a762400a07d9fb4e63f5c5aeb4
BLAKE2b-256 checksum
How to use checksums
fd275b4b4652173f6e9d532dbf9094bc3c8f69d5b6a8f9c53e33c64f618af4ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

Release history Release notifications | RSS feed

0.9.2

2 release files

0.9.1

2 release files

This release

0.9.0 This release

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.10

2 release files

0.7.9

2 release files

0.7.8

2 release files

0.7.7

2 release files

0.7.6

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.20

2 release files

0.6.19

2 release files

0.6.18

2 release files

0.6.17

2 release files

0.6.13

2 release files

0.6.12

2 release files

0.6.11

2 release files

0.6.10

2 release files

0.6.9

2 release files

0.6.8

2 release files

0.6.7

2 release files

0.6.6

2 release files

0.6.5

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page