Nexus Agent SDK for Python
Python SDK for Nexus Agent.
中文 · API reference · Examples
Installation
Python 3.11+.
pip install nexus-agent-sdk-python
Platform wheels include nxs and rg. Supported platforms: Linux, macOS and Windows on x86_64 and ARM64.
export ANTHROPIC_API_KEY="your-api-key"
Custom runtime:
from nexus_agent_sdk import NexusAgentOptions
options = NexusAgentOptions(cli_path="/path/to/nxs")
Quick Start
import asyncio
from nexus_agent_sdk import query
async def main():
async for message in query(prompt="What is 2 + 2?"):
print(message)
asyncio.run(main())
Providers
Anthropic Messages
import os
from nexus_agent_sdk import AnthropicProvider, NexusAgentOptions
options = NexusAgentOptions(
provider=AnthropicProvider(
api_key=os.environ["ANTHROPIC_API_KEY"],
base_url="https://api.anthropic.com",
),
model="claude-sonnet-4-6",
)
OpenAI
import os
from nexus_agent_sdk import NexusAgentOptions, OpenAIProvider
options = NexusAgentOptions(
provider=OpenAIProvider(
api_key=os.environ["OPENAI_API_KEY"],
base_url="https://api.openai.com/v1",
protocol="responses",
),
model=os.environ["OPENAI_MODEL"],
)
| Provider | Fields |
|---|---|
AnthropicProvider |
api_key, auth_token, base_url, version, headers, tool_discovery_transport |
OpenAIProvider |
api_key, base_url, protocol, org_id, project_id, headers |
protocol accepts chat_completions or responses. Use base_url for compatible services.
Provider fields override matching options.env entries. Unset fields inherit environment configuration.
Anthropic credentials use api_key or auth_token.
Provider example · Provider API
query()
import asyncio
from nexus_agent_sdk import AssistantMessage, NexusAgentOptions, TextBlock, query
async def main():
options = NexusAgentOptions(
system_prompt="You are a code reviewer.",
cwd="/path/to/project",
tools=["Read", "Glob", "Grep"],
allowed_tools=["Read", "Glob", "Grep"],
max_turns=3,
)
async for message in query(prompt="Review this project", options=options):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(block.text)
asyncio.run(main())
| Option | Purpose |
|---|---|
model |
Model name |
system_prompt |
System prompt |
cwd |
Working directory |
tools |
Built-in tool set |
allowed_tools |
Permission allowlist |
disallowed_tools |
Blocked tools |
permission_mode |
Permission policy |
max_turns |
Turn limit |
max_budget_usd |
Cost limit |
env |
Runtime environment variables |
NexusSDKClient
import asyncio
from nexus_agent_sdk import NexusSDKClient
async def main():
async with NexusSDKClient() as client:
await client.query("Remember the number 7")
print((await client.receive_result()).result)
await client.query("What number did I give you?")
async for message in client.receive_response():
print(message)
asyncio.run(main())
| Method | Purpose |
|---|---|
query(prompt) |
Submit a prompt or async iterable |
receive_response() |
Read through the next result |
receive_result() |
Return the result; raise ResultError on failure |
receive_messages() |
Read the message stream |
interrupt() |
Interrupt the current turn |
set_model(model) |
Change the model |
set_permission_mode(mode) |
Change the permission policy |
get_context_usage() |
Read context usage |
get_mcp_status() |
Read MCP server status |
Consume async iterable input with receive_messages() through completion before submitting another query.
Custom Tools
import asyncio
from nexus_agent_sdk import (
NexusAgentOptions,
NexusSDKClient,
create_sdk_mcp_server,
tool,
)
@tool("add", "Add two numbers", {"a": int, "b": int})
async def add(args):
return {"content": [{"type": "text", "text": str(args["a"] + args["b"])}]}
async def main():
server = create_sdk_mcp_server(name="calculator", tools=[add])
options = NexusAgentOptions(
mcp_servers={"calculator": server},
allowed_tools=["mcp__calculator__add"],
)
async with NexusSDKClient(options) as client:
await client.query("Use the calculator to add 12 and 30")
print((await client.receive_result()).result)
asyncio.run(main())
External MCP Servers
from nexus_agent_sdk import NexusAgentOptions
options = NexusAgentOptions(
mcp_servers={
"tools": {
"type": "stdio",
"command": "python",
"args": ["/path/to/mcp_server.py"],
}
}
)
Hooks
from nexus_agent_sdk import HookMatcher, NexusAgentOptions
async def log_tool_call(input_data, tool_use_id, context):
print(input_data["tool_name"], input_data["tool_input"])
return {}
options = NexusAgentOptions(
hooks={
"PreToolUse": [HookMatcher(hooks=[log_tool_call])],
}
)
Hooks and permission callbacks
Sessions
from nexus_agent_sdk import NexusAgentOptions, SessionStore
store = SessionStore()
for session in store.list_sessions():
print(session.session_id)
options = NexusAgentOptions(resume="session-id")
fork_options = NexusAgentOptions(resume="session-id", fork_session=True)
Types
| Types | Source |
|---|---|
NexusAgentOptions, AgentDefinition |
options.py |
AssistantMessage, UserMessage, SystemMessage, ResultMessage |
messages.py |
TextBlock, ThinkingBlock, ToolUseBlock, ToolResultBlock |
messages.py |
PermissionResultAllow, PermissionResultDeny, HookMatcher |
callbacks.py |
InitializationResult, ContextUsageResponse, McpStatusResponse |
control.py |
Error Handling
| Exception | Cause |
|---|---|
NexusSDKError |
SDK operation failure |
ProcessError |
Runtime startup or exit failure |
ProtocolError |
Invalid runtime output |
ControlError |
Rejected control request |
ResultError |
Error result from receive_result() |
BufferOverflowError |
Message queue limit exceeded |
Development
uv sync --frozen --group dev
uv run python -m compileall -q examples
uv run ruff check .
uv run ruff format --check .
uv run mypy src/nexus_agent_sdk
uv build
Source checkouts require nxs on PATH or cli_path.
License
Metadata
Release files for nexus-agent-sdk-python 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nexus_agent_sdk_python-0.1.0.tar.gz | 39.9 kB | Without provenance | Details for nexus_agent_sdk_python-0.1.0.tar.gz |
Built distributions (wheels)
Total release size: 48.1 MB
Release files / nexus_agent_sdk_python-0.1.0.tar.gz
| Download URL | nexus_agent_sdk_python-0.1.0.tar.gz |
|---|---|
| Size | 39.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eb0c655d67592772a7ffe5765ed75ff1b7bb32886fe79135396773d5b71c4932
|
|
BLAKE2b-256 checksum How to use checksums |
39d4bfc2856d314daa94bc7c02ce9df89a11e9d6eadb83889240977cd099ff31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.15
|
Release files / nexus_agent_sdk_python-0.1.0-py3-none-win_arm64.whl
| Download URL | nexus_agent_sdk_python-0.1.0-py3-none-win_arm64.whl |
|---|---|
| Size | 7.5 MB |
| Tags | Python 3 Windows ARM64 |
|
SHA-256 checksum How to use checksums |
1d6991fc351e3cefa6bf299a21b63144e096a65d209c8a7717e66c8c699de9b4
|
|
BLAKE2b-256 checksum How to use checksums |
50fdc47d78af21505a62ca8651bdadd280672bd68a1141f4430dc12476fa8102
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.15
|
Release files / nexus_agent_sdk_python-0.1.0-py3-none-win_amd64.whl
| Download URL | nexus_agent_sdk_python-0.1.0-py3-none-win_amd64.whl |
|---|---|
| Size | 8.3 MB |
| Tags | Python 3 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
1745d8b32e8110322f91283c61aed0ff7c06fd03273a4661b414145547414c27
|
|
BLAKE2b-256 checksum How to use checksums |
69ab2c4c23e3a2cca45ac1aefed0dd322b31a4598029b29bf203aa7d4249352a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.15
|
Release files / nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_x86_64.whl
| Download URL | nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 8.6 MB |
| Tags | Linux glibc 2.28+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
8d1fa0e1af0d1d307ce702d07da8510bb91b92216110766ed1899e4fb3e8b3ad
|
|
BLAKE2b-256 checksum How to use checksums |
6234b994005772dd521da1405dc758e7d9e66f8776fdc0883002008aedef1625
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.15
|
Release files / nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_aarch64.whl
| Download URL | nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 7.6 MB |
| Tags | Linux glibc 2.28+ ARM64 Python 3 |
|
SHA-256 checksum How to use checksums |
01c5722347975a6729a057dccc329e04e1d33edd07c2a61eb7d85c752540c85c
|
|
BLAKE2b-256 checksum How to use checksums |
5636ee015ef77121958f2a87361d40b17b3ed6dbbf501722e636c41734ace8c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.15
|
Release files / nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_x86_64.whl
| Download URL | nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_x86_64.whl |
|---|---|
| Size | 8.3 MB |
| Tags | Python 3 macOS 14.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
102934d13ca758cf6bb59b4e97257b6c662ff936a058c0117f4a76bb7fba3e99
|
|
BLAKE2b-256 checksum How to use checksums |
43a4b11aa045ad27fa93423aa82c571f9e96c24f616ce46e568545ca45b66133
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.15
|
Release files / nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_arm64.whl
| Download URL | nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_arm64.whl |
|---|---|
| Size | 7.7 MB |
| Tags | Python 3 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
1e550c69f70f5784c370e320bf3188e4ff312eb8ad1c6f8cc5a28c0635e285d5
|
|
BLAKE2b-256 checksum How to use checksums |
e89deda4db95febd3ab2a2772f9f6fd0d61846b5d68c7d448a5177e43c70f78d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.15
|
Security for nexus-agent-sdk-python 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.