Context Layer

Install Context Layer

Context Layer establishes a runtime boundary between applications and LLM providers. For agentic workflows, MCP is the primary integration surface (see the Quick start bonus step below). Provision Execution Authority in the console to generate the wrapper script for direct application integrations. The wrapper is not an SDK. You copy it into your codebase; you do not install a package.

Quick start

  1. Set CL_API_KEY
  2. Bonus: agentic workflows (MCP): use the MCP endpoint and reference repository (see below). MCP does not require the wrapper script or State Bridge.
  3. Import the wrapper
  4. Call invokeCL() for execution

Step 1: set API key

export CL_API_KEY="your_flow_api_key"

Bonus: MCP (agentic workflows)

For agent-driven and external integrations, Context Layer is available over MCP. This path uses the same CL_API_KEY and runtime enforcement as the wrapper. MCP does not require the generated wrapper script or State Bridge.

See MCP for send_message, validation rules, and flags.


Step 2: import wrapper

const { invokeCL } = require("./flow-wrapper");

Step 3: run Flow execution

Each invokeCL() call represents one workflow step.

const res = await invokeCL("Generate invoice");
console.log(res.output);

Step 4: final step and termination

Mark the final step of the workflow using workflowEnd.

const res = await invokeCL("Finalize invoice", { workflowEnd: true });

if (res.terminated) {
  console.log("Workflow completed");
}

Important

  • Flow steps execute sequentially
  • Concurrent invokeCL() calls are rejected
  • Direct model provider calls (OpenAI, Anthropic, etc.) are blocked

Runtime behavior

Context Layer automatically:

  • controls execution context
  • enforces constraints
  • verifies outputs
  • routes models
  • produces execution receipts

Note

Model selection is controlled by Context Layer routing and API key configuration.
Developers cannot select models per step.


Prerequisites

  • Node.js 18+ (or compatible runtime)
  • An LLM provider API key (OpenAI-compatible or Anthropic)
  • A Context Layer account

Step 1: provision execution authority

Create a project in the Context Layer console and click Provision Execution Authority. The console generates a wrapper script. Copy it into the project. Do not modify the generated script. The API key is obtained when you provision Execution Authority.

After provisioning, the console also shows MCP connection details: https://mcp.cl.kaisek.com and Context Layer MCP on GitHub. MCP does not use the generated wrapper or State Bridge; agents invoke execution through MCP instead.


Step 2: copy the wrapper script

Place the generated wrapper in the project. The wrapper enforces runtime execution rules. It exposes invokeCL().


Step 3: export cl_api_key

The wrapper requires the environment variable CL_API_KEY.

export CL_API_KEY=your_flow_api_key

or for Pulse Mode:

export CL_API_KEY=your_pulse_api_key

The wrapper throws an error if CL_API_KEY is not set.


State Bridge (optional)

cl-state-bridge.js is an optional utility. Copy it into your project alongside flow-wrapper.js or pulse-wrapper.js when the console provides it (or when you need structured-to-natural-language conversion). The module has no dependencies. It converts structured data from any external system into one natural-language string for invokeCL().

const { buildCLMessage, assertCLResult } = require("./cl-state-bridge");
const { invokeCL } = require("./flow-wrapper");

const msg = buildCLMessage(
  "Proceed with form submission.",
  { SubmitButton: { visible: true }, FormFields: { complete: true }, username: "john_doe" },
  { constraint: "SubmitButton must be visible before proceeding." }
);

const res = await invokeCL(msg);

assertCLResult(res, ["invoice", "totals"]);

Pass the full structured payload; keys are flattened with dot notation. Standard text-only workflows do not need State Bridge. See State Bridge.


Stateless execution

Stateless execution (optional)

Stateless calls run outside of a workflow session.
They do not accumulate workflow state.

await invokeCL("Quick validation", { stateless: true });

Constraint: stateless cannot be combined with { workflowEnd: true }.


Pulse usage

const { invokeCL } = require("./pulse-wrapper");

await invokeCL("User message");

Provider compatibility

Context Layer supports:

  • OpenAI-compatible providers (OpenAI, Grok)
  • Anthropic

Developers use their own provider SDK in their application. Context Layer never accesses developer LLM API keys.


Runtime contract checklist

Before using the wrapper:

  • Final workflow step MUST include { workflowEnd: true }
  • Concurrent invokeCL() calls are rejected
  • Direct model provider calls are blocked
  • Stateless executions bypass workflow state
  • Context Layer never accesses developer LLM API keys

Next steps