Skip to main content

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

Options

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.

Client API · Streaming input

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)

Session API

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

Error types

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.

Build and release · Changelog

License

Apache-2.0 · Third-party notices

Download files

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

Source Distribution

nexus_agent_sdk_python-0.1.0.tar.gz (39.9 kB view details)

Uploaded Source

Built Distributions

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

nexus_agent_sdk_python-0.1.0-py3-none-win_arm64.whl (7.5 MB view details)

Uploaded Python 3Windows ARM64

nexus_agent_sdk_python-0.1.0-py3-none-win_amd64.whl (8.3 MB view details)

Uploaded Python 3Windows x86-64

nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_x86_64.whl (8.6 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_aarch64.whl (7.6 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_x86_64.whl (8.3 MB view details)

Uploaded Python 3macOS 14.0+ x86-64

nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_arm64.whl (7.7 MB view details)

Uploaded Python 3macOS 14.0+ ARM64

File details

Details for the file nexus_agent_sdk_python-0.1.0.tar.gz.

File metadata

  • Download URL: nexus_agent_sdk_python-0.1.0.tar.gz
  • Upload date:
  • Size: 39.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.15

File hashes

Hashes for nexus_agent_sdk_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eb0c655d67592772a7ffe5765ed75ff1b7bb32886fe79135396773d5b71c4932
MD5 76b77f15ea0b8e09770eef1bc1c3cbcf
BLAKE2b-256 39d4bfc2856d314daa94bc7c02ce9df89a11e9d6eadb83889240977cd099ff31

See more details on using hashes here.

File details

Details for the file nexus_agent_sdk_python-0.1.0-py3-none-win_arm64.whl.

File metadata

File hashes

Hashes for nexus_agent_sdk_python-0.1.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 1d6991fc351e3cefa6bf299a21b63144e096a65d209c8a7717e66c8c699de9b4
MD5 81c32a406527f99ad82c4f48b7ac468d
BLAKE2b-256 50fdc47d78af21505a62ca8651bdadd280672bd68a1141f4430dc12476fa8102

See more details on using hashes here.

File details

Details for the file nexus_agent_sdk_python-0.1.0-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for nexus_agent_sdk_python-0.1.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 1745d8b32e8110322f91283c61aed0ff7c06fd03273a4661b414145547414c27
MD5 837dcfbcb93984695f12b32007c775f2
BLAKE2b-256 69ab2c4c23e3a2cca45ac1aefed0dd322b31a4598029b29bf203aa7d4249352a

See more details on using hashes here.

File details

Details for the file nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8d1fa0e1af0d1d307ce702d07da8510bb91b92216110766ed1899e4fb3e8b3ad
MD5 0bf426d69c88c96e04af32c9d867490a
BLAKE2b-256 6234b994005772dd521da1405dc758e7d9e66f8776fdc0883002008aedef1625

See more details on using hashes here.

File details

Details for the file nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for nexus_agent_sdk_python-0.1.0-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 01c5722347975a6729a057dccc329e04e1d33edd07c2a61eb7d85c752540c85c
MD5 26492d5a958ea8fae192d88369805b0d
BLAKE2b-256 5636ee015ef77121958f2a87361d40b17b3ed6dbbf501722e636c41734ace8c2

See more details on using hashes here.

File details

Details for the file nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_x86_64.whl.

File metadata

File hashes

Hashes for nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_x86_64.whl
Algorithm Hash digest
SHA256 102934d13ca758cf6bb59b4e97257b6c662ff936a058c0117f4a76bb7fba3e99
MD5 295da5707a459ec2b9d697a8c0a285bb
BLAKE2b-256 43a4b11aa045ad27fa93423aa82c571f9e96c24f616ce46e568545ca45b66133

See more details on using hashes here.

File details

Details for the file nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for nexus_agent_sdk_python-0.1.0-py3-none-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 1e550c69f70f5784c370e320bf3188e4ff312eb8ad1c6f8cc5a28c0635e285d5
MD5 6e209aa026fb3c9119d9dbe934c080f2
BLAKE2b-256 e89deda4db95febd3ab2a2772f9f6fd0d61846b5d68c7d448a5177e43c70f78d

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

7 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