Skip to main content

selfconnect — Python SDK

Python client and optional framework adapters for SelfConnect session, budget-status, and event APIs.

pip install selfconnect

Quick Start

3-Line Integration

from selfconnect import TskClient

client = TskClient(tsk_key="sc-tsk-YOUR-KEY")
with client.session("my-agent") as session_id:
    client.post_event(session_id, "llm_call", tokens_input=512, tokens_output=128)

Core Concepts

TskClient presents a server-issued TSK credential to a configured SelfConnect API. The server can accept or reject session and event requests, including returning 401, 403, or 429; direct client calls surface those responses as typed exceptions. The SDK does not itself establish hardware binding, cryptographic identity, storage immutability, event completeness, or a deployment authorization.

Concept Description
TSK Key Server-issued bearer credential (sc-tsk-XXXX-YYYY)
Session A bounded unit of work — start → events → end
Event A caller-reported event such as an LLM call, tool use, or policy decision
Budget Server-reported token budget; direct calls raise on a 429 response
Hash chain Server-reported retained event chain; useful for tamper detection within its stated boundary

Installation

# Core SDK
pip install selfconnect

# With LangChain support
pip install selfconnect[langchain]

# Development
pip install selfconnect[dev]

Requirements: Python ≥ 3.9, httpx >= 0.24


Usage

Basic Session Lifecycle

from selfconnect import TskClient

client = TskClient(tsk_key="sc-tsk-YOUR-KEY")

# Manual lifecycle
session_id = client.start_session("research-agent", meta={"env": "production"})
client.post_event(session_id, "llm_call", tokens_input=1024, tokens_output=256)
client.post_event(session_id, "tool_use", meta={"tool": "web_search"})
client.end_session(session_id, summary="Research task completed")

Context Manager (Recommended)

with client.session("research-agent") as session_id:
    result = llm.invoke("Summarize the latest AI governance news")
    client.post_event(session_id, "llm_call", tokens_input=512, tokens_output=200)
# Session auto-ends on exit, even if an exception is raised

Decorator

@client.governed_session("data-pipeline-agent")
def run_pipeline(session_id: str, dataset: str) -> dict:
    client.post_event(session_id, "tool_use", meta={"tool": "data_loader", "dataset": dataset})
    # ... your agent logic ...
    return {"status": "complete"}

result = run_pipeline("sales_q4_2025")

Async Support

@client.governed_session("async-agent")
async def run_async_agent(session_id: str, query: str) -> str:
    client.post_event(session_id, "llm_call", tokens_input=256, tokens_output=128)
    return "result"

result = await run_async_agent("What is AI governance?")

Batch Events

events = [
    {"session_id": session_id, "event_type": "llm_call", "tokens_input": 512, "tokens_output": 128},
    {"session_id": session_id, "event_type": "tool_use", "meta": {"tool": "calculator"}},
    {"session_id": session_id, "event_type": "policy_check", "decision": "approved"},
]
client.post_events(events)

Budget Monitoring

budget = client.get_budget()
print(f"Used: {budget['used']:,} / {budget['budget']:,} tokens ({budget['pct_used']:.1f}%)")
print(f"Remaining: {budget['remaining']:,} tokens")

Server-Reported Workflow Data

workflow = client.get_session_workflow(session_id)
for event in workflow["chain_of_custody"]:
    print(f"[{event['event_type']}] hash={event['entry_hash'][:16]}...")

LangChain Integration

from langchain_openai import ChatOpenAI
from selfconnect import SelfConnectCallbackHandler

handler = SelfConnectCallbackHandler(
    tsk_key="sc-tsk-YOUR-KEY",
    agent_id="langchain-research-agent",
)

llm = ChatOpenAI(model="gpt-4o", callbacks=[handler])
response = llm.invoke("Explain AI governance in 3 sentences")

print(f"Session ID: {handler.session_id}")

What the callback adapter attempts to report:

  • LLM callbacks received while a SelfConnect session is active
  • Tool callbacks received while a SelfConnect session is active
  • Agent action/finish callbacks received by the handler
  • Chain error callbacks and bounded error text

raise_on_error=False is the default. In that mode, SelfConnect API failures are logged and LangChain execution continues, so the adapter is fail-open telemetry rather than a non-bypassable enforcement boundary. Set raise_on_error=True when the caller requires callback delivery errors to stop execution.


CrewAI Integration

from crewai import Agent, Crew, Task
from selfconnect.integrations.crewai_example import GovernedCrew

crew = Crew(agents=[researcher, writer], tasks=[research_task, write_task])

governed = GovernedCrew(
    tsk_key="sc-tsk-YOUR-KEY",
    crew=crew,
    agent_id="content-creation-crew",
)
result = governed.kickoff(inputs={"topic": "AI governance"})
print(f"Session ID: {governed.session_id}")

AutoGen Integration

import autogen
from selfconnect.integrations.autogen_example import GovernedConversation

conv = GovernedConversation(
    tsk_key="sc-tsk-YOUR-KEY",
    initiator=user_proxy,
    recipient=assistant,
    agent_id="autogen-research",
)
result = conv.initiate_chat(
    message="Analyze the competitive landscape for AI governance tools",
    max_turns=5,
)
print(f"Session ID: {conv.session_id}")

Error Handling

from selfconnect import TskClient, BudgetExhaustedError, TskInvalidError, SelfConnectError

try:
    client.post_event(session_id, "llm_call", tokens_input=1000)
except BudgetExhaustedError:
    # TSK budget exhausted — agent must stop
    print("Budget exhausted. Request a top-up from your SelfConnect admin.")
except TskInvalidError:
    # Key revoked or invalid
    print("TSK key is invalid or has been revoked.")
except SelfConnectError as e:
    # Other API errors
    print(f"API error {e.status_code}: {e}")

API Reference

TskClient

Method Description
start_session(agent_id, meta=None) Start a session, returns session_id
end_session(session_id, summary=None) End a session
post_event(session_id, event_type, ...) Post a single event
post_events(events) Post a batch of events
get_budget() Get current budget status
get_session_workflow(session_id) Get server-reported workflow and event hash-chain data
get_tsk_info() Get metadata for this TSK key
get_tsk_events(limit=100) Get recent events for this key
session(agent_id) Context manager — auto start/end
governed_session(agent_id) Decorator — auto start/end
close() Close HTTP connection pool

SelfConnectCallbackHandler

Parameter Default Description
tsk_key required Your TSK key
agent_id "langchain-agent" Agent identifier
auto_session True Auto-start/end sessions
session_id None Attach to existing session
raise_on_error False Re-raise SDK errors

Environment Variables

Variable Description
SELFCONNECT_TSK_KEY Default TSK key (used by integration tests)
SELFCONNECT_BASE_URL Override API base URL

Testing

# Unit tests only (no network required)
pytest tests/ -m "not integration"

# All tests including live API
SELFCONNECT_TSK_KEY=sc-tsk-YOUR-KEY \
SELFCONNECT_BASE_URL=https://your-disposable-test-api.example \
pytest tests/ -m integration -v

Evidence and Authorization Boundary

The SDK can retrieve server-reported events and hash-chain fields that an operator may include in a broader evidence package. It does not by itself establish EU AI Act, ISO 42001, NIST SP 800-53, FIPS 140, FedRAMP, ATO, or DoD Impact Level compliance or authorization. Those conclusions depend on the deployed server, complete system boundary, configuration, custody, assessment, and approving authority.

Hash chaining can reveal modification of retained entries. It does not by itself prove that all events were captured, prevent truncation/deletion, bind events to a signer, or make storage immutable.

Credential Storage

Prefer SELFCONNECT_TSK_KEY supplied by an operating-system or CI secret store. selfconnect login writes the credential to ~/.selfconnect/config.json. Although the CLI requests owner-only file mode where supported, chmod(0600) is not a Windows ACL guarantee. Do not treat the local JSON file as a hardware- backed or independently attested credential store.


Links


License

MIT © SelfConnect.ai

Release files for selfconnect 1.1.3

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

Source distribution (sdist)

Source distribution for selfconnect 1.1.3
File Size Uploaded
selfconnect-1.1.3.tar.gz 27.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for selfconnect 1.1.3
File Interpreter ABI Platform
selfconnect-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 53.1 kB

Release files / selfconnect-1.1.3.tar.gz

Download URL selfconnect-1.1.3.tar.gz
Size 27.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2595393d1a0a099d6d194a321156610a540702d195c56feb152961c3f64b406e
BLAKE2b-256 checksum
How to use checksums
4b0bad404dd6d24c11cff4a9640634a7d5281a0d834767960faf494a19039660
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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 Jul 17, 2026.

Transparency log

Release files / selfconnect-1.1.3-py3-none-any.whl

Download URL selfconnect-1.1.3-py3-none-any.whl
Size 25.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c19f21e4afc8beccee496e8673a1c038a7b20b19d28970f6112202d7aafd7dd6
BLAKE2b-256 checksum
How to use checksums
c93145812ae94f0d0c46ca6b979cacb192b2225a721237eac263ae50c85ba508
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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 Jul 17, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.3 This release

2 release files

1.1.0

2 release files

1.0.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