Python SDK¶
The SDK embeds the same agent loop as the CLI and TUI.
from tynex.sdk import Agent
agent = Agent(model="anthropic/claude-sonnet-4-6", coding_tools=True)
result = agent.run("Summarize this repository.")
print(result.text)
print(result.cost)
Tools¶
from tynex.sdk import Agent, tool
@tool
def search_orders(customer: str, limit: int = 10) -> list[dict]:
"""Find recent orders for a customer."""
return []
agent = Agent(model="anthropic/claude-sonnet-4-6", tools=[search_orders])
@tool derives JSON Schema from type hints and reads descriptions from a Google-style Args: section. Async functions are supported.
Permissions and approvals¶
from tynex.agent.permissions import PermissionRequest
def approve(request: PermissionRequest) -> str: # "allow", "session" or "deny"
return "allow" if request.subject.startswith("git ") else "deny"
agent = Agent(
model="anthropic/claude-sonnet-4-6",
coding_tools=["read", "bash"],
permission_mode="ask",
approval=approve,
)
SDK defaults are permission_mode="bypass", no checkpoints, no MCP, no project discovery, and no session file. Opt in explicitly for anything you need.
Streaming and events¶
for event in agent.stream("list the failing tests"):
if event["type"] == "message_update":
print(event, end="")
arun() and astream() are async equivalents. on_event= receives the same events as the CLI, and RunResult exposes usage, cost, messages, tool calls, stop reason, and budget state.
MCP and context¶
from tynex.sdk import Agent, McpServerConfig
agent = Agent(
model="anthropic/claude-sonnet-4-6",
mcp_servers=[McpServerConfig(name="time", command="uvx", args=["mcp-server-time"])],
context=["Team conventions live in docs/style.md."],
coding_tools=["read", "grep", "ls"],
)
See examples/sdk_agent.py for a complete custom-tool, streaming, approval, and budget example.