Skip to main content

workspaceguard-cli (Python)

Per-workspace usage metering and quota caps for one shared self-hosted AI assistant deployment -- a genuine, independent Python port of the workspaceguard-cli npm package, not a wrapper around a Node binary.

PyPI version License: MIT Python versions

Why this exists

Run Odysseus (or a compatible self-hosted assistant) for your whole household or small team, and there is no way to see who sent how many messages this month, or to stop one person's usage from burning through everyone else's API budget. WorkspaceGuard is a thin sidecar that adds that layer: per-workspace message counts, optional monthly caps that fail closed, and a CLI report an admin (or another agent) can read.

This project originally set out to add per-user workspace isolation (separate chat history, memory, API keys) to a self-hosted AI chat platform. A feasibility spike found that Odysseus already enforces per-user ownership on chat history, memory, and API tokens by default, so building a competing isolation layer would have duplicated work Odysseus already does correctly. This project instead keeps its tested isolation engine (namespace separation, an AES-256-GCM vault with real key rotation, fail-closed identity resolution, a self-healing circuit breaker) as the identity-resolution substrate, and builds the layer Odysseus does not provide: usage metering and quota enforcement per workspace. See the project README for the full story.

Install

pip install workspaceguard-cli

Current status: this Python port is fully built, tested (50/50 pytest tests passing), and published to PyPI. pip install workspaceguard-cli works today -- see pypi.org/project/workspaceguard-cli. The npm package (workspaceguard-cli) is also published and installable today via npm install -g workspaceguard-cli -- see npmjs.com/package/workspaceguard-cli. This package is a genuine, independent port -- not a wrapper around the npm package -- so it works and is maintained regardless of the npm package's status.

Quickstart

# Register the workspaces sharing one deployment (identity = the header
# value your reverse proxy sets after authenticating, e.g. Cloudflare
# Access).
workspaceguard add-workspace alex --identity alex@example.com
workspaceguard add-workspace jordan --identity jordan@example.com

# Optional: cap alex at 1000 messages/month. Omit for unlimited (the default).
workspaceguard set-cap alex 1000

# See usage for every workspace.
workspaceguard usage
$ workspaceguard usage --json
{"ok": true, "usage": [{"workspaceId": "alex", "identity": "alex@example.com", "monthlyMessageCap": 1000, "percentUsed": 0, "period": "2026-07", "messageCount": 0, "estimatedBytes": 0}]}

CLI reference

Every command accepts --json for a structured, agent-native output shape instead of the human-readable text shown below. Identical command surface to the npm CLI.

Command What it does
workspaceguard init Initializes the data directory and vault for this deployment.
workspaceguard add-workspace <id> --identity <value> Registers a workspace, idempotent on repeat calls for the same id.
workspaceguard status [--json] Lists configured workspaces.
workspaceguard usage [--json] Per-workspace message count, cap, and percent-used for the current month.
workspaceguard set-cap <id> <count|none> Sets or clears a workspace's monthly message cap.
workspaceguard rotate-key <id> Rotates a workspace's vault encryption key (invalidates the old ciphertext).
workspaceguard scan [--json] Isolation config scan (scaffold stub, carried over from the original build).
workspaceguard -h, --help Prints the command list above and exits.
workspaceguard -V, --version Prints the installed package version and exits.

Global options

Option What it does
--data-dir <path> Data directory for config, vault, and usage data. Takes precedence over WORKSPACEGUARD_DATA_DIR.
--force init only: regenerate the master key even if an existing key file at the resolved data dir looks corrupted or truncated. Warning: permanently invalidates anything already encrypted under the old key.
--json Structured, agent-native output instead of human-readable text.

Data directory resolution, in order: --data-dir flag, then WORKSPACEGUARD_DATA_DIR env var, then ~/.workspaceguard. This used to default to the current working directory with no override -- running init from the wrong shell could silently write a live encryption key into an unrelated directory. init on an existing, valid key is idempotent (it loads and reuses that key); init on a key file that exists but doesn't decode to a valid key refuses to overwrite it without --force.

Both -h/--help and -V/--version work as documented on the currently published release. See CHANGELOG.md for release history.

Library API

import asyncio
from workspaceguard import create_workspace_guard, MockAdapter, QuotaExceededError

async def main():
    guard = await create_workspace_guard(data_dir="./data", backend=MockAdapter())
    await guard.add_workspace("alex", "alex@example.com")
    await guard.set_cap("alex", 1000)

    try:
        await guard.chat("alex@example.com", "hello")
    except QuotaExceededError:
        pass  # alex is over their monthly cap

    report = await guard.usage_report()

asyncio.run(main())

The library API is async (asyncio), matching the async architecture of the original TypeScript source rather than flattening it to synchronous calls.

MCP Server

This package ships a Model Context Protocol server, so an MCP-compatible agent (Claude Desktop, Claude Code, an orchestrator) can call WorkspaceGuard directly as a tool instead of shelling out to the CLI and parsing text.

pip install "workspaceguard-cli[mcp]"

It exposes one tool, run, a generic subprocess wrapper: pass it the same argument list you'd pass on the command line, and it shells out to the installed workspaceguard binary, parses the resulting JSON, and returns it. Every failure mode (missing binary, launch error, timeout, non-zero exit, unparseable output) comes back as a plain {"error": ...} dict instead of raising, so a bad call can't crash the server.

run(args=["usage", "--json"])
# -> {"ok": true, "usage": [{"workspaceId": "alex", "identity": "alex@example.com", "monthlyMessageCap": 1000, "percentUsed": 0, "period": "2026-08", "messageCount": 0, "estimatedBytes": 0}]}

To register it with an MCP-compatible client such as Claude Desktop, add it to the client's server config:

{
  "mcpServers": {
    "workspaceguard": {
      "command": "workspaceguard-mcp"
    }
  }
}

This assumes workspaceguard-mcp is already on PATH (installed via the mcp extra above). If you installed it somewhere else, replace "command" with the full path to the console script.

How it works

target: an inbound chat request with an identity header value
   -> resolve_workspace() -- fail closed on any miss, never a default workspace
   -> check_quota() -- QuotaExceededError if the workspace is at its cap
   -> circuit breaker -- calls the BackendAdapter, opens after 3 consecutive
      failures, self-heals via a half-open probe after a cooldown
   -> record usage -- per-workspace, per-month counters with automatic
      period rollover
  • workspaceguard/isolation_guard.py -- the single choke point (chat()) every request flows through: resolve workspace -> check quota -> call backend -> record usage.
  • workspaceguard/usage.py -- the usage-metering engine this project adds: per-workspace, per-month counters with automatic period rollover, and QuotaExceededError enforcement.
  • workspaceguard/vault.py, workspaceguard/namespace.py, workspaceguard/circuit_breaker.py -- the original isolation-engine code, kept as the identity/workspace-boundary substrate the metering layer reads from, not shipped as a competing isolation product.
  • workspaceguard/adapters/ -- BackendAdapter abstract base class; a real Odysseus HTTP adapter is the next step (currently MockAdapter only, same as the TypeScript original).

Backend-specific behavior never enters the core modules directly -- everything goes through BackendAdapter.

Trust boundary

WorkspaceGuard trusts an upstream identity header (default: Cf-Access-Authenticated-User-Email) to resolve the workspace. It must never be directly reachable from the network -- only from behind whatever trusted proxy sets that header (Cloudflare Access, Tailscale, etc.). This is documented, not code-enforced.

What's real vs. not yet built

  • Real, tested (50/50 pytest tests passing): usage metering, quota enforcement, the original isolation engine (vault, namespace separation, circuit breaker), CLI with --json mode.
  • Not yet built: a real Odysseus HTTP adapter (only MockAdapter exists so far, same as the TypeScript original), a hosted managed billing dashboard (deliberately out of scope for this MIT project).

Security

The vault uses AES-256-GCM (via the cryptography package) with one derived key per workspace per generation -- rotating a workspace's key increments its generation, so old ciphertext genuinely can no longer be decrypted, not a no-op. See SECURITY.md for the disclosure process and what's in/out of scope.

Contributing

See CONTRIBUTING.md.

cd python
python3 -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest

License

MIT, see LICENSE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

workspaceguard_cli-0.1.7.tar.gz (28.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

workspaceguard_cli-0.1.7-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file workspaceguard_cli-0.1.7.tar.gz.

File metadata

  • Download URL: workspaceguard_cli-0.1.7.tar.gz
  • Upload date:
  • Size: 28.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.13

File hashes

Hashes for workspaceguard_cli-0.1.7.tar.gz
Algorithm Hash digest
SHA256 3512b65ac9b0743054e03e51fd2ce4dfd2415be421ab919cdccc2a361547eb6d
MD5 fd9ab38aadb7af674a9f3628da19ebbb
BLAKE2b-256 b294a22da8ae64e8afaa014b2be4994bfced64ccecbdc5e38827faf9ab87fe59

See more details on using hashes here.

File details

Details for the file workspaceguard_cli-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for workspaceguard_cli-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a715cee740ecbc4c2d5a040fdb3ec05189f4d791f21cde2f68744da5bd4f7c30
MD5 6d6d8fde8e85ad62a9703c1cd3dc78e5
BLAKE2b-256 2f3fc85203c1bcdea1f9652eb8a84374a8d73377a553cf6e427a1cb53dbf41bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page