$ fssg build --deterministic --validate |

How to Add FACET to an Existing Agent Without Rewriting It

FACET

You do not need to rewrite an agent to give it a compiled contract boundary.

The safest way to introduce FACET into an existing system is deliberately unglamorous: leave the provider SDK, tool implementations, business logic, and orchestration loop where they are. Replace one unstable seam—the code that assembles prompts, tool schemas, policy, and runtime values into a request—with a compiler call that produces canonical JSON.

This is a migration guide for that narrow change. It does not turn FACET into an agent framework, and it does not claim deterministic model output. It gives an existing agent a request-construction boundary that can be built, tested, inspected, pinned, and rolled back like code.

The seam to replace

A typical agent already has four layers, even if one function currently hides all of them:

application data
      ↓
prompt/tool assembly
      ↓
provider adapter
      ↓
agent loop and tool dispatch

The migration changes only the second layer:

application data
      ↓
FACET contract + typed runtime input
      ↓
canonical JSON
      ↓
existing provider adapter and agent loop

The canonical object—not the .facet source and not a provider request—is the handoff. According to the FACET integration guide, its minimum useful surface is metadata, tools, and messages. An optional execution artifact carries audit data. This preserves a clean division of responsibility: FACET constructs and checks the request; the host still owns sockets, credentials, retries, response validation, tool execution, and the loop.

That distinction is important. FACET's run command compiles one request and stops. The repository's agent-loop documentation explicitly keeps the multi-turn loop in the host. Adopting FACET therefore does not require adopting a new orchestration framework.

Step 1: freeze the current request as a fixture

Before changing production code, capture one representative request after your current assembly function and before the provider SDK transforms it. Remove secrets and unstable fields such as request IDs or timestamps. Keep the semantic fields: system and user messages, tool names and schemas, model-facing metadata, and the runtime values that produced them.

This fixture has two jobs. First, it prevents a migration from silently changing behavior while everyone is looking at syntax. Second, it forces you to separate request construction from provider transport. If you cannot capture this boundary without invoking a model, the old integration is already too entangled.

Do not demand byte equality between the legacy request and FACET output unless both formats define the same canonicalization rules. Compare the properties that matter:

  • message roles, order, and content;
  • exposed tool names and argument schemas;
  • required policy decisions;
  • runtime values after declared transformations;
  • context budget and overflow behavior.

This is a semantic compatibility fixture, not a screenshot test for incidental JSON formatting.

Step 2: move runtime data through @input

The host should pass values; it should not rewrite the contract. Put user text, tenant-safe configuration, retrieved data, and other per-run values in a JSON input file or the equivalent process boundary:

@meta
  name: "support-answer"

@context
  budget: 32000

@vars
  question: @input(type="string") |> trim()
  account_context: @input(type="string", default="")

@system
  content: "Answer from the supplied account context. Do not invent missing facts."

@user
  content: "Question: $question\n\nAccount context: $account_context"
{
  "question": "Why is my invoice still pending?",
  "account_context": "Invoice 1842: status=pending_review"
}

Then compile the boundary:

facet-fct build --input contracts/support-answer.facet
facet-fct run \
  --input contracts/support-answer.facet \
  --runtime-input runtime.json \
  --format json \
  --exec

Typed runtime inputs are the host-facing interface of the contract. I described that boundary in Typed Runtime Inputs: The ABI of a FACET Contract. The practical migration rule is simpler: if a value changes per request, pass it as data. If it defines request architecture, keep it in the contract.

Step 3: adapt canonical JSON, not FACET semantics

The adapter should be boring. It maps canonical messages and tools into the provider SDK's request types, sends the call, and normalizes the response. It must not reorder messages, reinterpret policy, repair schemas, or invent defaults.

A useful adapter contract looks like this:

compile(contract, runtime_input) -> canonical_payload, execution_artifact
adapt(canonical_payload)         -> provider_request
send(provider_request)           -> provider_response
validate(provider_response)      -> model_turn

Keep provider-only controls—credentials, endpoint, transport timeout, and SDK retry settings—in the host. If a provider needs an extension that FACET does not represent, make that extension explicit and test it. Do not smuggle semantic decisions into a mapping function.

This preserves the rule from Provider Adapters Must Not Think: portability fails when each adapter becomes a second compiler.

Step 4: keep the loop and effect boundary in the host

After the first model response, your existing loop still decides what happens next. When the model requests a tool, the host must resolve the declared interface, evaluate the runtime guard, execute only an allowed call, validate the result, and feed that result into the next turn.

FACET's current compiler documentation demonstrates this with a scripted client. It also states two limits plainly: the shipped loop client is scripted rather than a real provider adapter, and tool results are appended to the payload instead of recompiling every turn through the document. Those limits matter. The repository is evidence for the contract and reference path; it is not evidence that every production loop is already integrated for you.

For a brownfield migration, that is acceptable because the host already owns the loop. Start by compiling turn zero. Keep your existing guarded dispatch and response validation. Move later turns behind the same compiled boundary only when their state transition is represented honestly.

Step 5: add a shadow gate before cutover

Run the new compiler path beside the old assembly path without sending the FACET-produced request to the model. For each safe fixture:

  1. build and test the contract;
  2. compile with the same runtime values;
  3. compare the semantic request surface;
  4. archive the canonical output and inspection views;
  5. fail the migration check on unexplained drift.

FACET exposes AST, dependency graph, layout, and policy views through inspect:

facet-fct inspect \
  --input contracts/support-answer.facet \
  --ast artifacts/ast.json \
  --dag artifacts/dag.json \
  --layout artifacts/layout.json \
  --policy artifacts/policy.json

This is different from the FACET contract gate in CI. CI proves that the new contract is valid. The shadow gate proves that replacing the old assembler preserves the behavior you intended to keep.

Cut over one route, tenant, or task class at a time. Rollback should be a routing change back to the old assembler, not an emergency edit to the contract.

The minimum production record

For every downstream request, record enough identity to connect transport logs to compiled intent:

{
  "contract": "support-answer",
  "compiler_version": "<pinned version>",
  "document_hash": "<hash>",
  "policy_hash": "<hash>",
  "provider_request_id": "<id>",
  "execution_artifact": "<private artifact reference>"
}

Do not put prompts, customer data, credentials, or the full artifact into ordinary logs by default. Store a reference under the retention and access rules appropriate to the data. The purpose is correlation: when a provider response is disputed, you can identify which contract and policy produced the request.

Pinning the compiler is part of that identity. A moving branch makes compiler behavior an ambient dependency. Upgrade it as a reviewed change, rerun fixtures, and retain the previous binary or image until rollback is no longer needed.

What success looks like

A successful first integration is intentionally narrow:

  • one existing request path uses a .facet contract;
  • per-run values enter through typed inputs;
  • canonical JSON is the only semantic handoff to the adapter;
  • the existing provider SDK and agent loop remain in place;
  • CI compiles and tests the contract;
  • shadow fixtures detect unexplained request drift;
  • hashes connect downstream requests to compiled artifacts;
  • rollback does not require rewriting production code.

FACET does not need to own the whole stack to improve it. A compiler earns its place by making one boundary stricter than the boundary it replaced.

Start there. Replace string assembly, not the system. Once the request is a compiled artifact, every later migration becomes a choice instead of a leap.

Sources

FACET AI CLI Terminal
facet@ai:~$