Thin runtime wrapper around the Swytchcode CLI
Project description
swytchcode-runtime (Python)
Thin runtime wrapper around the Swytchcode CLI. Calls swytchcode exec for you so you can stay in Python without shell boilerplate.
Requires: The swytchcode CLI must be installed. The binary is located automatically — no configuration needed in most environments. Resolution order:
SWYTCHCODE_BINenv var — explicit override.$PATHlookup viashutil.which— the standard system resolution.- Common install paths —
~/.local/bin,/usr/local/bin(Unix) or%LOCALAPPDATA%\Programs\swytchcode\bin(Windows).
Install
pip install swytchcode-runtime
Or from the repo:
pip install /path/to/runtime-libraries/python-runtime
Use
JSON mode (default)
from swytchcode_runtime import exec
result = exec("api.account.create", {"email": "test@example.com"})
# result is parsed JSON (any)
Equivalent to: swytchcode exec api.account.create --json with args on stdin.
Request input (args): The second argument is the kernel args object (sent as JSON on stdin). Use this shape so the kernel builds the request correctly:
- body — Request body (dict).
- params — Query/path params (e.g.
{"id": "cluster-123"}). - Authorization — Auth header value (e.g.
"Bearer token123"). - headers — Additional request headers (e.g.
{"X-Request-Id": "abc-123"}). - Other top-level keys are passed as query params.
Example with body, params, and headers:
exec("api.cluster.get", {
"params": {"id": "cluster-123"},
"Authorization": "Bearer token123",
"headers": {"X-Request-Id": "abc-123"},
})
Raw mode
Get stdout as a string instead of parsing JSON:
from swytchcode_runtime import exec
output = exec("api.report.export", {"id": "123"}, raw=True)
# output is the raw stdout string
Options
- cwd – Working directory for the process (default: current directory).
- env – Extra environment variables (merged with
os.environ). - raw – If
True, use--rawand return stdout as a string. - dry_run – If
True, pass--dry-runto the CLI; request details (method, url, headers, body) are output instead of calling the server. - allow_raw – If
True, pass--allow-rawto the CLI; required for executing raw methods (kernel has this disabled by default).
This runtime invokes swytchcode exec [canonical_id] with the flags above. For full exec behavior, see the Swytchcode kernel documentation.
Environment variables
| Variable | Description |
|---|---|
SWYTCHCODE_BIN |
Override the resolved binary path. Set this only when automatic resolution does not find the correct binary (e.g. non-standard install locations or virtualised environments). |
SWYTCHCODE_TOKEN |
Auth token passed to the CLI via the process environment. |
Error handling
Failures raise SwytchcodeError (spawn error, non-zero exit, invalid JSON). Use the helper to narrow:
from swytchcode_runtime import exec, SwytchcodeError, is_swytchcode_error
try:
exec("api.account.create", {"email": "test@example.com"})
except Exception as e:
if is_swytchcode_error(e):
print(e.message, e.cause)
raise
What this library is
- A thin process wrapper over the Swytchcode shell.
- A stable call surface:
exec(canonical_id, input=None, *, cwd=..., env=..., raw=...). - A convenience for developers and IDE agents.
What this library is not
- An HTTP SDK or API client.
- A replacement for the shell. All authority stays in the Swytchcode CLI.
This library must not and does not:
- Read
.swytchcode/*or parsetooling.json. - Validate schemas or add retries/timeouts.
- Call the registry or embed auth logic.
It is a pipe, not a brain.
For Cursor / IDE agents
Generate code that uses this runtime instead of subprocess boilerplate.
Preferred:
from swytchcode_runtime import exec
def create_account(input: dict) -> any:
return exec("api.account.create", input)
Avoid: subprocess boilerplate, HTTP calls, or config parsing. Use exec(...) and let the CLI handle execution and policy.
Agentic workflows (framework integrations)
On top of exec, the runtime exposes a small agentic surface that turns Swytchcode tools
into the native tool objects each agent framework expects. The Swytchcode part is the same
two lines regardless of framework:
from swytchcode_runtime import Swytchcode
from swytchcode_runtime.providers.anthropic import AnthropicProvider
swx = Swytchcode(provider=AnthropicProvider())
tools = swx.tools.get(toolkits=["stripe"]) # framework-native tools, full param schemas
Selecting tools — swx.tools.get(...)
Pass exactly one selector; IDs resolve against your local Swytchcode state and remote search:
toolkits=["stripe"]— every enabled tool whose integration matches a toolkit.tools=["charges.charge.create"]— explicit canonical IDs.search="refund a charge"— natural-language discovery (viaswytchcode discover).
Each returned tool carries a full input schema — every field is surfaced to the model, with
only the truly-required ones marked required — and an execute callback that runs swytchcode exec for you (empty optional values are stripped before the call so APIs like Stripe don't reject them).
Supported providers
| Framework | Import | Who runs the tool loop |
|---|---|---|
| Anthropic Claude | providers.anthropic.AnthropicProvider |
you (Messages API + swx.handle_tool_calls) |
| OpenAI Agents SDK | providers.openai_agents.OpenAIAgentsProvider |
the SDK |
| Vercel AI SDK | providers.vercel.VercelProvider |
the SDK |
| LangGraph | providers.langgraph.LangGraphProvider |
the prebuilt agent |
| CrewAI | providers.crewai.CrewAIProvider |
the crew |
Non-agentic APIs (Anthropic Messages)
When you run the tool loop yourself, handle_tool_calls executes each tool_use block and
returns the tool_result blocks to send back:
import anthropic
client = anthropic.Anthropic()
msg = client.messages.create(
model="claude-sonnet-5", max_tokens=1024, tools=tools,
messages=[{"role": "user", "content": "Refund charge ch_123 for $20"}],
)
results = swx.handle_tool_calls(msg) # runs the tool calls, returns tool_result blocks
One runnable file per framework lives in sdk-examples/. Install the matching framework SDK
(pip install openai-agents / anthropic / ai / langgraph / crewai) alongside the
swytchcode CLI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swytchcode_runtime-1.0.0.tar.gz.
File metadata
- Download URL: swytchcode_runtime-1.0.0.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a36367549eec74516641e3e124e529e68c9fbcf1d8b989058d35237e4dbcb7a9
|
|
| MD5 |
fb698a72916297d57931b03db171bdb6
|
|
| BLAKE2b-256 |
30d99f7af907673be347d504c9f77137521b39d649d4f21148cd1deb6b103a88
|
Provenance
The following attestation bundles were made for swytchcode_runtime-1.0.0.tar.gz:
Publisher:
publish-pypi.yml on swytchcodehq/runtime-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swytchcode_runtime-1.0.0.tar.gz -
Subject digest:
a36367549eec74516641e3e124e529e68c9fbcf1d8b989058d35237e4dbcb7a9 - Sigstore transparency entry: 2142368199
- Sigstore integration time:
-
Permalink:
swytchcodehq/runtime-py@db40cb818aaa7911ec8d2bd4826dbf932637fff6 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/swytchcodehq
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@db40cb818aaa7911ec8d2bd4826dbf932637fff6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file swytchcode_runtime-1.0.0-py3-none-any.whl.
File metadata
- Download URL: swytchcode_runtime-1.0.0-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f06975976533dc3200e668d7072c0f14b84354fadb754f80a92d2d3f519210d
|
|
| MD5 |
8a6c83ce25e39ef6bf83a69428b95abc
|
|
| BLAKE2b-256 |
5968df1cff89413d7d70a1821bf733d2fdbf7bd9a71d159ef48ead2acb4c7fb1
|
Provenance
The following attestation bundles were made for swytchcode_runtime-1.0.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on swytchcodehq/runtime-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swytchcode_runtime-1.0.0-py3-none-any.whl -
Subject digest:
7f06975976533dc3200e668d7072c0f14b84354fadb754f80a92d2d3f519210d - Sigstore transparency entry: 2142368749
- Sigstore integration time:
-
Permalink:
swytchcodehq/runtime-py@db40cb818aaa7911ec8d2bd4826dbf932637fff6 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/swytchcodehq
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@db40cb818aaa7911ec8d2bd4826dbf932637fff6 -
Trigger Event:
push
-
Statement type: