anthropics / anthropics/claude-agent-sdk-python
some suggestions
- 主要語言
- Python
- 星號
- 8.1k
- 分支
- 1.3k
- PR 合併指標
- PR 指標待擷取
描述
Suggestions for enhancing the enterprise-level capabilities of Claude Agent SDK
The Claude Agent SDK has already played a good role as the agent execution kernel, with practical capabilities such as model invocation, tool invocation, MCP, hooks, subagent, permission, session resume, etc. To make the SDK more suitable for enterprise-level production systems, it is recommended to further strengthen the following directions.
1. Enhance the lifecycle management of Agent Run
Enterprise systems typically need to manage "user request generation" as an independent operational unit, rather than just a single SDK call.
It is recommended that the SDK provides a first-class `AgentRun` model:
```text
run_id
session_id
parent_run_id
user_message_id
status
started_at
updated_at
completed_at
cancel_reason
error_category
retryable
```
Suggested built-in standard status:
```text
queued
running
streaming
waiting_for_permission
waiting_for_user_input
cancelling
cancelled
completed
failed
expired
```
Meanwhile, it is recommended to support:
- run heartbeat
- terminal event guarantee
- Idempotent run start
- run cancel
- run timeout
- run retry
- run resume
- run audit trail
The most common issues in enterprise systems arise when gateways are disconnected, services are restarted, or consumers are interrupted. In such cases, the business side is unsure whether the SDK is currently "still generating", "has failed", "retryable", or "completed". If the SDK natively provides a run lifecycle, it will significantly reduce the complexity of the upper-level system.
**2. Provide a more complete session storage abstraction
The current session mechanism leans more towards transcript resume. Enterprise systems often require the storage of:
- User message
- assistant message
- tool call
- tool result
- permission decision
- run state
- Intermediate event
- Reason for error
- Model selection
- Context compression record
- Multi-terminal recovery state
It is recommended that the SDK be extended to a more comprehensive storage interface based on the existing session store:
```python
class AgentStore:
async def save_session(...)
async def load_session(...)
async def append_message(...)
async def list_messages(...)
async def save_run(...)
async def update_run(...)
async def append_event(...)
async def list_events(...)
```
In this way, enterprises can integrate with infrastructures such as Postgres, Redis, MongoDB, S3, Kafka, and object storage, rather than solely relying on local transcripts or repeatedly encapsulating outside the SDK.
**3. Standardized event protocol**
Enterprise front-ends, gateways, audit systems, and monitoring systems typically require consuming unified events, rather than parsing internal SDK objects.
It is recommended that the SDK clearly defines a stable event schema, for example:
```text
message_delta
message_completed
tool_call_started
tool_call_delta
tool_call_completed
tool_call_failed
permission_requested
permission_resolved
run_started
run_updated
run_completed
run_failed
run_cancelled
context_compacted
subagent_started
subagent_completed
```
Each event suggestion should include:
```text
event_id
event_type
run_id
session_id
sequence
timestamp
payload
schema_version
```
And provide:
- JSON Schema
- typed Python model
- event replay
- event resume from sequence
- SSE/WebSocket/gRPC friendly format
- Backward compatibility strategy
This will make it easier for the SDK to integrate with the enterprise's existing message bus, real-time push, audit logs, and observable systems.
**4. Provide Middleware Pipeline**
Hooks are valuable, but enterprise systems often require a more systematic interception chain.
It is suggested that the SDK provides a middleware pipeline:
```text
before_run
before_context_build
after_context_build
before_model_call
after_model_call
before_tool_call
after_tool_call
before_permission_check
after_permission_check
before_event_emit
after_event_emit
on_error
on_run_complete
```
Typical enterprise scenarios include:
- Permission verification
- Data desensitization
- Sensitive word check
- Prompt injection protection
- Tool whitelist control
- Output protocol verification
- Cost statistics
- trace injection
- Audit data storage
- Error classification
- Automatic retry strategy
If the SDK incorporates a middleware model, the upper-level system does not need to scatter packaging logic everywhere.
**5. Enhance the abstraction of Permission Policy
The core issue for enterprise-level agents is not "whether they can call tools", but "what tools can be called in what capacity, under what context, and at what risk level".
It is recommended that the SDK provide a stronger permission policy:
```python
class PermissionPolicy:
async def evaluate_tool_call(context, tool_call) -> PermissionDecision
async def evaluate_file_access(context, path, operation) -> PermissionDecision
async def evaluate_network_access(context, target) -> PermissionDecision
```
`PermissionDecision` Recommendation support:
```text
allow
deny
ask_user
require_admin
allow_once
allow_for_session
allow_for_workspace
```
And support:
- policy composition
- tool risk level
- workspace-level allowlist
- user-level permission
- organization-level permission
- approval audit log
- dry-run permission check
This will make the SDK more suitable for enterprise permission, auditing, and compliance scenarios.
**6. Enhance context management capability
The context of enterprise agents typically originates from multiple sources:
- Current user messages
- Historical dialogues
- Business system status
- Knowledge base retrieval
- File content
- Tool execution result
- User personas
- Permission information
- Current business process status
It is recommended that the SDK provides a composable context source abstraction:
```python
class ContextSource:
async def load(context_request) -> ContextFragment
```
And provide context-based budget management:
```text
priority
token_budget
compress_strategy
expiration
visibility
source_type
```
The enterprise system needs to clearly understand "which contexts have been incorporated into the model, why they were incorporated, how many tokens they occupy, whether they are compressed, and whether they are auditable.".
**7. Improve error classification and recovery semantics
The production system needs to stably distinguish between:
```text
model_overloaded
rate_limited
network_error
tool_error
permission_denied
context_too_large
invalid_output
sdk_internal_error
user_cancelled
timeout
```
It is recommended to standardize the error types in the SDK and clarify:
- Is it retryable
- Whether it can be restored
- Should it be displayed to the user
- Should the degradation model be triggered
- Should the run lock be released
- Is manual intervention required
-
This will reduce the occurrence of string parsing errors in enterprise systems.
**8. Provide Regenerate / Branch Session API**
In actual products, "regeneration" is a high-frequency capability. Enterprise systems need to regenerate based on a certain user message or assistant message, while retaining historical branches.
Suggested native SDK support:
```python
regenerate(session_id, from_message_id)
branch_session(session_id, from_message_id)
truncate_session(session_id, after_message_id)
```
And clarify the handling methods of transcript, tool result, permission decision, and subagent results in the branches.
**9. Provide Workspace / Runtime Snapshot**
When investigating problems, enterprises need to know which runtime configurations were used during a single run.
Suggest that the SDK supports runtime snapshot:
```text
model
fallback_model
tools
mcp_servers
subagents
permission_mode
cwd
environment
hooks
middleware
system_prompt_hash
context_sources
sdk_version
cli_version
```
Each run solidifies a snapshot, facilitating auditing, playback, and problem localization.
**10. Enhance Observability**
It is recommended that the SDK integrates built-in support for OpenTelemetry, covering at least:
- run duration- model latency- tool latency- token usage- retry count- error category- subagent duration- permission latency- context size- event count
And provide standard trace/span:
```textagent.runagent.context.buildagent.model.callagent.tool.callagent.permission.checkagent.event.emitagent.subagent.run```
This is crucial for enterprises to investigate slow requests, cost anomalies, tool failures, and model downgrades.
**Priority Suggestion**
P0:
- AgentRun lifecycle - standard event protocol - run cancel / timeout / heartbeat - error classification - regenerate / branch session
P1:
- middleware pipeline- permission policy- context source abstraction- runtime snapshot
P2:
- Complete AgentStore- OpenTelemetry deep integration - enterprise audit and compliance extension
**Core demands**
The Claude Agent SDK does not need to be transformed into a complete business framework, but it can provide a more stable enterprise-level runtime boundary:
```textAgent execution kernel+ typed lifecycle+ typed events+ pluggable storage+ pluggable middleware+ pluggable permission policy+ observable runtime```
This way, enterprises can build reliable Agent products on top of the SDK, rather than having each team repeatedly implement the infrastructure such as session, run, lock, event, permission, retry, audit, and regenerate.
貢獻指南
這個儲存庫沒有索引到貢獻指南
評估
這個 Issue 還沒有評估資料。