-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy path40_acp_agent_example.py
More file actions
50 lines (39 loc) · 1.65 KB
/
40_acp_agent_example.py
File metadata and controls
50 lines (39 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Example: Using ACPAgent with Claude Code ACP server.
This example shows how to use an ACP-compatible server (claude-agent-acp)
as the agent backend instead of direct LLM calls. It also demonstrates
``ask_agent()`` — a stateless side-question that forks the ACP session
and leaves the main conversation untouched.
Prerequisites:
- Node.js / npx available
- ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY set (can point to LiteLLM proxy)
Usage:
uv run python examples/01_standalone_sdk/40_acp_agent_example.py
"""
import os
from openhands.sdk.agent import ACPAgent
from openhands.sdk.conversation import Conversation
agent = ACPAgent(acp_command=["npx", "-y", "@zed-industries/claude-agent-acp"])
try:
cwd = os.getcwd()
conversation = Conversation(agent=agent, workspace=cwd)
# --- Main conversation turn ---
conversation.send_message(
"List the Python source files under openhands-sdk/openhands/sdk/agent/, "
"then read the __init__.py and summarize what agent classes are exported."
)
conversation.run()
# --- ask_agent: stateless side-question via fork_session ---
print("\n--- ask_agent ---")
response = conversation.ask_agent(
"Based on what you just saw, which agent class is the newest addition?"
)
print(f"ask_agent response: {response}")
# Report cost (ACP server reports usage via session_update notifications)
cost = agent.llm.metrics.accumulated_cost
print(f"EXAMPLE_COST: {cost:.4f}")
finally:
# Clean up the ACP server subprocess
agent.close()
cost = conversation.conversation_stats.get_combined_metrics().accumulated_cost
print(f"\nEXAMPLE_COST: {cost}")
print("Done!")