Skip to main content

swfte-nexus-sdk

Agent observability and enforcement for Python services you run yourself.

PyPI Python Licence Required dependencies

nexus wrap captures what an agent does inside a developer's terminal. This package captures the same thing inside your own application — the third attach point on one event ledger, stamped producer="sdk" so a run in production and a run on a laptop are the same shape in the same tables.

pip install swfte-nexus-sdk        # import name is `nexus`

Python 3.10+. Zero required dependencies — see Why no dependencies.

Part of Nexus by Swfte — savings, governance and a real audit trail for AI agents. See also the Node SDK (@swfte/nexus-sdk), which emits the same events, and the terminal wrapper (npm i -g @swfte/nexus).


Contents

Section
Three levels, pick one the whole API, in increasing detail
Guarantees what this SDK promises never to do to your process
Configuration every variable, the precedence rule, and the privacy tiers
Deployment shapes gunicorn, uWSGI, Lambda, gevent, Kubernetes, and the rest
Why no dependencies and what is vendored instead
Status what is implemented and what is not

Reference documentation

Document For
docs/API.md every public name, its signature, and what it returns
docs/OVERHEAD.md measured cost, the method, and the budgets CI enforces
docs/RUNTIME-CASES-CHECKLIST.md every deployment shape and its coverage status
docs/SPECS.md the internal design documents the source comments cite
AGENTS.md orientation for coding agents working in this repository
CONTRIBUTING.md how to run the suite, and what review will ask of a change
SECURITY.md reporting a vulnerability
CHANGELOG.md what changed in each release, and why

Three levels, pick one

Level 1 — no code changes. Prefix the command:

nexus-run python -m yourapp

Or set it once for a container, with no wrapper process at all:

PYTHONPATH=$(python -c "import nexus.bootstrap,os;print(os.path.dirname(nexus.bootstrap.__file__))")

nexus-run execs your program rather than spawning it as a child, so signals, exit codes and process identity are yours, unchanged.

Level 2 — declare who you are. One call, at startup:

import nexus
nexus.init(service="checkout", env="prod", version="2026.8.1")

Datadog's unified tagging. service / env / version land on every event as join keys. There is no user identity anywhere in this SDK: a service does not have a user, and inventing one is how a telemetry pipeline becomes a compliance problem.

Level 3 — say what the agent did.

with nexus.agent("refund-request", goal_class="transaction") as run:
    with run.action("db_write", target="refunds") as act:
        act.effect(rows=1, amount_cents=4200)

    run.usage(model="claude-opus-4", input_tokens=1_800, output_tokens=240)
    run.outcome("success", verified=True, verified_by="ledger_balance")

A run is a unit of agent work with an outcome. An action is something with an effect on the world — the thing enforcement will eventually gate. The distinction is not stylistic: what the agent did (behavior_trace) and what the model said it did (rationalisation) are different epistemic classes and are stamped as such, because only one of them is admissible as evidence.

All three levels compose. Auto-init plus explicit init() plus manual spans is a supported combination; init() called twice, or ten times from a notebook cell, upgrades identity in place.


Guarantees

A telemetry SDK must never be the reason a request fails. Concretely, and each one is a test:

  • Every public entry point is wrapped in a guard that contains exceptions and returns a safe default. Coverage is exhaustive by construction — guards register themselves, and the chaos suite iterates the registry, so a new entry point without a guard fails the build.
  • The calling thread never performs I/O. Asserted by spying on socket.socket.connect across a realistic workload, not inferred from timings.
  • The queue is bounded and drops oldest, because the newest events describe the incident. Every drop is counted and reported in nexus.counters(). Silence about dropped data is a bug.
  • fork() is handled: the child replaces its locks rather than acquiring them (an inherited held lock is a deadlock in a pre-fork server), empties the inherited queue, takes a fresh session, and rebuilds its worker. Parent-buffered events are not replayed once per worker.
  • Shutdown flushes with a deadline. A hung collector must not hang your container.
  • SIGKILL loses the in-flight buffer. That is not fixable and we do not pretend otherwise — test_4_9_sigkill_loses_the_buffer_and_we_say_so asserts the loss.

Measured cost: ~19 µs per action span, ~35 ms to arm the SDK at startup, ~0 ms for import nexus itself. Full numbers, method, and the budgets enforced in CI: docs/OVERHEAD.md.

The kill switch is real

NEXUS_ENABLED=0

No threads, no import hooks, no sockets, and nothing initialised-then-no-op'd. nexus/__init__.py imports only os at module scope — not even typing, because on a cold interpreter import typing pulls in re and lands tens of milliseconds in the cold-start budget of every Lambda that has this installed and switched off. A disabled with nexus.agent(...) block costs 0.5 µs.


Configuration

Explicit arguments win over environment variables, which win over a config file, which wins over defaults. The one exception is NEXUS_ENABLED=0, which wins over everything — an operator disabling telemetry from outside the process has to beat application code that says otherwise, or it is not a kill switch.

There is no ~/.nexus. A file is read only when NEXUS_CONFIG_FILE names one, and failing to read it is a warning, never an exception.

Variable Default
NEXUS_ENABLED 1 0 disables entirely
NEXUS_SERVICE / NEXUS_ENV / NEXUS_VERSION unknown unified tagging
NEXUS_COLLECTOR_URL http://127.0.0.1:8791 full URL
NEXUS_COLLECTOR_HOST / NEXUS_COLLECTOR_PORT — sidecar mode: not hardcoded to loopback, IPv6 hosts are bracketed
NEXUS_TIER metadata_only metadata_only / redacted_preview / full
NEXUS_QUEUE_CAPACITY 10000 events, then drop-oldest
NEXUS_FLUSH_INTERVAL_S / NEXUS_FLUSH_DEADLINE_S 2.0 / 5.0
NEXUS_TRANSPORT_MODE auto thread / sync; auto-detects uWSGI without --enable-threads
Tier What goes on the wire
metadata_only Shape, counts, durations, and *_fingerprint digests. No content.
redacted_preview The above, plus a redacted, truncated plaintext preview of message text.
full The above, with the text at its configured length limit rather than the preview limit.

No field is hashed except *_fingerprint. The middle tier was originally spelled hashed, which was wrong in the way that matters — it reads as "one-way digest" to exactly the person most likely to choose it on the strength of the word, and what it emits is a sentence with the recognisable parts struck out. [REDACTED] is applied by pattern matching, and a pattern matches a shape, not a meaning. At this tier "Patient Jane Doe, SSN 123-45-6789, said: my password is hunter2" leaves as "Patient Jane Doe, SSN [REDACTED], said: my password is hunter2" — the national ID goes, because it has a shape; the name stays, because it does not; and so does the password, because the credential patterns are anchored to an assignment (password = "hunter2" is caught, "my password is hunter2" is prose). Read that example as the specification, not as a worst case. If your requirement is that identifiable content never leaves the process, the tier that meets it is metadata_only, and that is why it is the default. hashed is still accepted in configuration as an alias and still what travels on the wire, so existing deployments and collectors need no change.

Nothing leaves the process above the configured tier. metadata_only is the default because the first time an SDK ships prompt text to a collector by accident is the last time it is allowed in that company.

Enforcement

Every agent-observability product observes. This one can refuse.

from nexus import policy

# Decide, and branch yourself. Never raises.
d = policy.decide("tool_action", {"tool": "db.write", "target": "prod-orders"})
if d.denied:
    return "not allowed"

# Or let the gate do it. The body never runs if an enforcing rule denies.
with policy.gate("tool_action", {"tool": "db.write", "target": "prod-orders"}):
    write_the_rows()

policy.install(signed_envelope)      # signed, verified, versioned

The decision happens before the body, not around it. A denial raised after the write already went out is not enforcement, it is journalism. gate decides and only then yields; test_enforcement.py::test_deny_lands_before_the_effect asserts on a side-effect list that stays empty, and inverting the order is exactly what makes it fail.

What raises. Denied is the only exception this SDK will ever put in your traceback, and you opted into it twice — the rule carried enforce: true and the call site did not decline. Everything else is contained: a bug in policy evaluation degrades to allow plus an integrity alert, never to a 500 in your service. decide() never raises at all.

Failure semantics, which is the part that has to be exactly right:

Situation Behaviour
Policy missing, unverifiable or unparseable allow, and raise an integrity alert — the gap is visible, not merely survived
Policy verified but stale enforce-marked rules keep denying; unmarked rules degrade to advice
Evaluation exceeds its latency budget allow, and record the timeout
A rule requires human approval and times out deny if enforce-marked, allow if not

Denying because we could not reach something is never a default anywhere in this package. A cached deny never decays, though — otherwise disconnecting from the control plane would be the documented bypass.


Deployment shapes

Gunicorn (pre-fork and --preload), uWSGI, uvicorn/hypercorn, celery, multiprocessing (fork and spawn), threads, asyncio, gevent (before or after monkey.patch_all()), AWS Lambda, Cloud Run / Fargate, Kubernetes with a sidecar collector, python -m, Jupyter, and frozen builds.

Each has a test, and where the runtime cannot be installed in CI the test drives the mechanism the SDK actually depends on and says so in its name. docs/RUNTIME-CASES-CHECKLIST.md records the status of every case, including the ones not covered.

Comments in the source cite a few internal design documents (DEPUTY.md, F3-SDK-RUNTIME-CASES.md, REPLICATION-EFFORT.md) and a WP-N phase shorthand. docs/SPECS.md explains what each one is and which tests carry the parts that govern behaviour here.

AWS Lambda needs one decorator, because the sandbox freezes between invocations and a background flusher never runs:

@nexus.instrument_lambda_handler(budget_s=1.0)
def handler(event, context):
    ...

Why no dependencies

The core ring declares nothing. Not requests, not urllib3, not wrapt.

This is a product decision, not asceticism. An observability SDK is adopted by a team that already has a resolver graph they are afraid of, and a version conflict at pip install time is where the adoption conversation ends. wrapt and dd-trace-py's import-hook machinery are vendored under src/nexus/vendor/ with their licences and notices carried alongside — see NOTICE. A CI job asserts the installed distribution has no required dependencies, so this cannot regress quietly.

Optional extras: swfte-nexus-sdk[otel] (an OTLP bridge — HTTP/protobuf only, never gRPC, because grpcio is a C extension with fork-safety problems in exactly the pre-fork servers this SDK has to survive) and swfte-nexus-sdk[integrations].

Status

Honest about the difference between implemented, a seam, and not here. Nothing in this package is stubbed in a way that pretends to work.

Capability Status Where
Capture — runs, actions, usage, outcomes implemented nexus/api.py
Auto-attach — nexus-run, sitecustomize implemented nexus/bootstrap/, nexus/auto.py
Redaction ladder and privacy tiers implemented nexus/redact.py
Policy enforcement — a gate that can refuse implemented nexus/policy/
Signed policy envelopes, kill switch, approvals implemented nexus/policy/envelope.py, ed25519.py, approval.py
OTLP bridge ([otel] extra) implemented nexus/otel/
Node SDK, and conformance between the two implemented nexus-sdk-node
Provider auto-instrumentation (WP-5) the registry, not the adapters nexus/integrations/

nexus/integrations/ deliberately ships zero adapters. What ships is the shape an adapter must fit — independently versioned, its own patch/unpatch, its own tests — because that shape is what stops the next milestone from becoming a monolith. Adding a library must not be a core change.

The Node SDK, and staying identical to it

@swfte/nexus-sdk (Node) instruments the same ledger, and a service written in either language has to be indistinguishable on the dashboard except for the language — any difference a customer can see is a defect, not a language quirk.

That is checked rather than asserted. nexus-sdk-node/conformance/ runs one scripted scenario through both SDKs — two interpreters of a single scenario.json, both posting to a stub collector that speaks the real ingest contract — and diffs the emitted event streams field by field, at every privacy tier, against each other and against the 51 $defs of contract/events.v1.json. It is wired into a cross-sdk job in this repository's CI as well as that one, because an instrument that guards one direction guards nothing: the drift simply lands in whichever repository has no job for it. nexus-sdk-node/PARITY.md §9 describes what it covers, and §3b lists every difference that deliberately remains, with the reason for each.

Where the two are allowed to differ, they differ in interpretation of a caller's argument rather than on the wire — a bare-number watermark is epoch seconds here (time.time()) and epoch milliseconds there (Date.now()), because picking one would make the other language's idiomatic call wrong.

Nexus, beyond this package

This SDK is one attach point of three. All three write the same events to the same ledger, so a run in production and a run on a laptop are the same shape in the same tables.

Install What it attaches to
Terminal wrapper npm i -g @swfte/nexus · pip install swfte-nexus coding agents in a developer's terminal — Claude Code, Codex
Python SDK — this package pip install swfte-nexus-sdk your own Python services
Node SDK npm i @swfte/nexus-sdk your own Node services

Licence

Apache-2.0. See LICENSE and NOTICE — the latter carries the attribution for the vendored wrapt and dd-trace-py import-hook machinery.

Built by Swfte AI.

Metadata

Release files for swfte-nexus-sdk 0.1.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 swfte-nexus-sdk 0.1.0
File Size Uploaded Provenance Actions
swfte_nexus_sdk-0.1.0.tar.gz 364.6 kB Without provenance Details for swfte_nexus_sdk-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for swfte-nexus-sdk 0.1.0
File Interpreter ABI Platform Actions
swfte_nexus_sdk-0.1.0-py3-none-any.whl Python 3 none any Details for swfte_nexus_sdk-0.1.0-py3-none-any.whl

Total release size: 640.6 kB

Release files / swfte_nexus_sdk-0.1.0.tar.gz

Download URL swfte_nexus_sdk-0.1.0.tar.gz
Size 364.6 kB
Tags Source
SHA-256 checksum
How to use checksums
539f5af508a18b8c4ffccc71305af3346ecfb6bdf18aa049658fd8ff9aae5821
BLAKE2b-256 checksum
How to use checksums
af098da2da106035db6612a5eb1df3191a8c3781d2772c2304bbb6ab9fb9c559
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / swfte_nexus_sdk-0.1.0-py3-none-any.whl

Download URL swfte_nexus_sdk-0.1.0-py3-none-any.whl
Size 276.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f1503839f75fe37001e99db5a18808b85dbb3fd54487d632deef10f8ac3b8d7e
BLAKE2b-256 checksum
How to use checksums
57550f1724500e72909f9933fa2593d82400d16f9e7a38db0a353b657580d841
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for swfte-nexus-sdk 0.1.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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