4. Examples¶
Comprehensive examples demonstrating SW4RM SDK usage across Python, JavaScript/TypeScript, Rust, and Common Lisp implementations. For domain-specific application patterns, see Use Cases.
4.1. LLM-Integrated Multi-Agent Systems¶
Real-world examples showing agents using Claude (Anthropic) for reasoning and task execution.
-
Backend Agent + Frontend Agent
Two-agent system with LLM orchestration: Scheduler dispatches tasks to specialized agents (frontend/backend) who use Claude CLI to generate code, make decisions, and execute commands.
Features:
- Uses Claude API for reasoning and code generation
- CONTROL message orchestration via Scheduler
- Agents call LLM to confirm/adjust execution commands
- Generates full-stack applications (backend API + frontend UI)
- Real file I/O and subprocess execution
Available in:
- Python -
client_server_llm/{backend,frontend}_agent.py - TypeScript -
{backend,frontend}_agent.ts - Rust -
{backend,frontend}_agent.rs
Prerequisites: Install and authenticate the Claude CLI
See the Reference Services section for detailed setup instructions.
4.2. SDK Feature Examples¶
Protocol demonstrations showing SDK capabilities without LLM dependencies (useful for testing and learning the protocol).
Basic Examples¶
-
Echo Agent
Simple agent demonstrating registration and message echoing
Available in:
-
Advanced Agent
Full-featured agent with persistence, worktree management, and ACK lifecycle
Available in:
- Python (Three-ID & ActivityBuffer)
- TypeScript
- Rust
- Common Lisp
-
Test Client
Client for testing agent functionality with various message types
Available in:
Multi-Agent Patterns¶
-
Negotiation Room
Producer-critic-coordinator pattern with confidence-weighted voting
Available in:
-
Agent Handoff
Agent-to-agent task delegation with capability matching and context transfer
Available in:
-
Workflow Orchestration
DAG-based workflow with parallel execution and dependency management
Available in:
Advanced Features¶
-
HITL Escalation
Human-in-the-loop decision making for uncertainty and conflict resolution
Available in:
-
Voting & Aggregation
Confidence-weighted voting with multiple aggregation strategies
Available in:
-
Tool Streaming
Streaming tool call execution with real-time progress updates
Available in:
-
Secret Management
Multi-backend secret resolver with environment variable and file-based storage
Available in:
-
Three-ID Envelope Model
Message envelope construction with message_id, correlation_id, and idempotency_token
Available in:
-
:material-buffer:{ .lg .middle } Activity Buffer
Activity tracking with persistence and ACK lifecycle management
Available in:
4.3. Running the Examples¶
Python Examples¶
# Navigate to Python SDK
cd sdks/py_sdk
# Basic echo agent
python examples/echo_agent.py --agent-id echo-1
# Advanced patterns
python examples/negotiation_debate_example.py
python examples/workflow_orchestration_example.py
python examples/handoff_example.py
python examples/hitl_escalation_example.py
# Feature-specific
python examples/voting_example.py
python examples/tool_streaming_example.py
python examples/three_id_demo.py
JavaScript/TypeScript Examples¶
# Navigate to JavaScript SDK
cd sdks/js_sdk
# Basic echo agent
npx tsx examples/echoAgent.ts --agent-id echo-1
# Advanced agent with full features
npx tsx examples/advancedAgent.ts --data-dir ./agent_data
# Multi-agent patterns
npx tsx examples/negotiationRoomExample.ts
npx tsx examples/workflowExample.ts
npx tsx examples/handoffExample.ts
npx tsx examples/hitlEscalation.ts
Rust Examples¶
# Navigate to Rust SDK
cd sdks/rust_sdk
# Basic echo agent
cargo run --example echo_agent
# Advanced patterns
cargo run --example advanced_agent
cargo run --example negotiation_room
cargo run --example workflow
cargo run --example handoff
# Feature-specific
cargo run --example activity_demo
Common Lisp Examples¶
# Navigate to Common Lisp SDK
cd sdks/cl_sdk
# Echo agent
sbcl --load examples/echo-agent.lisp
# Advanced agent (ACK lifecycle, persistence, worktree)
sbcl --load examples/advanced-agent.lisp
# Negotiation voting
sbcl --load examples/negotiation-voting.lisp
# Tool streaming (tool client, connector)
sbcl --load examples/tool-streaming.lisp
# Secret management
sbcl --load examples/secret-management.lisp
4.4. Example Categories¶
Core SDK Features¶
These examples demonstrate fundamental SDK capabilities:
- Agent Registration: How to register agents with the registry service
- Message Routing: Sending and receiving messages via the router
- ACK Lifecycle: Implementing the full acknowledgment state machine
- Persistence: Saving and restoring agent state across restarts
- Worktree Binding: Managing agent workspace isolation
Multi-Agent Coordination¶
These examples show how agents collaborate:
- Negotiation: Producer-critic-coordinator pattern with voting
- Handoff: Delegating tasks between specialized agents
- Workflow: Orchestrating complex multi-step processes
- HITL: Escalating decisions to human operators
Advanced Patterns¶
These examples demonstrate sophisticated use cases:
- Confidence-Weighted Voting: Aggregating decisions with uncertainty
- Tool Streaming: Real-time tool execution with progress updates
- Three-ID Envelope Model: Message deduplication and correlation
- Activity Buffer: Comprehensive message tracking and reconciliation
4.5. Prerequisites¶
Before running examples, ensure you have the appropriate runtime and dependencies:
4.6. Step-by-Step Walkthroughs¶
Detailed walkthroughs for understanding and extending the Python SDK examples.
Echo Agent Walkthrough¶
File: sdks/py_sdk/examples/echo_agent.py
Concepts: Agent registration, message streaming, signal handling, graceful shutdown
Prerequisites¶
- Running Registry service on localhost:50052
- Running Router service on localhost:50051
- Generated protobuf stubs (
make protos)
Step-by-Step¶
- Parse arguments (lines 31-37) - Agent ID, name, router/registry addresses from command line or environment
- Create gRPC channels (lines 44-45) - Insecure channels to Router and Registry services
- Create clients (lines 47-48) -
RegistryClientandRouterClientinstances - Register agent (lines 51-65) - Submit
AgentDescriptorwith capabilities to Registry - Set up signal handler (lines 67-73) - Catch SIGINT for graceful shutdown
- Stream messages (lines 76-97) - Use
router.stream_incoming(agent_id)for message loop - Echo DATA messages (lines 86-97) - Build response envelope with same payload
- Graceful shutdown (lines 98-105) - Deregister from Registry on exit
Key Code Sections¶
# Registration (lines 51-63)
descriptor = {
"agent_id": args.agent_id,
"name": args.name,
"capabilities": ["echo"],
"communication_class": 2, # STANDARD
}
registry.register(descriptor)
# Message loop (lines 76-97)
for item in router.stream_incoming(args.agent_id):
if stop:
break
if msg.message_type == 2: # DATA
env = build_envelope(
producer_id=args.agent_id,
message_type=2,
payload=msg.payload,
correlation_id=msg.correlation_id,
)
router.send_message(env)
Common Issues¶
| Issue | Solution |
|---|---|
| "Protobuf stubs not generated" | Generate protobuf stubs in sdks/py_sdk/ (command below). |
| Connection refused | Ensure Router/Registry services are running |
| Agent not receiving messages | Verify agent_id matches sender's target |
Three-ID Demo Walkthrough¶
File: sdks/py_sdk/examples/three_id_demo.py
Concepts: Message envelope, Three-ID model, deduplication, workflow correlation
Prerequisites¶
- SDK installed (
pip install -e ".[dev]") - No services required (standalone demo)
Step-by-Step¶
- Envelope lifecycle (lines 21-45) - Create envelope, transition through states
- Three-ID semantics (lines 48-101) - Demonstrate message_id vs correlation_id vs idempotency_token
- Deduplication (lines 104-161) - Use ActivityBuffer with idempotency tokens
- Workflow correlation (lines 164-208) - Link messages in a workflow
Key Concepts¶
| ID | Scope | Changes on Retry |
|---|---|---|
message_id | Per-attempt | Yes (new UUID) |
correlation_id | Workflow/session | No (stable) |
idempotency_token | Operation | No (stable for dedup) |
Key Code Sections¶
# Generate idempotency token (lines 60-65)
op_hash = compute_deterministic_hash(operation_params)
idem_token = make_idempotency_token("agent-dev", "git_commit", op_hash)
# Deduplication check (lines 137-149)
existing = buffer.get_by_idempotency_token(token)
if existing:
if is_terminal_state(existing.state):
# Return cached result
...
else:
# Reject - operation in progress
...
Negotiation Debate Walkthrough¶
File: sdks/py_sdk/examples/negotiation_debate_example.py
Concepts: Multi-agent negotiation, producer-critic-coordinator pattern, voting, decision policies
Prerequisites¶
- SDK installed (
pip install -e ".[dev]") - No services required (uses in-memory NegotiationRoomClient)
Step-by-Step¶
- Create agents (lines 364-370) - Producer, Critics (with expertise), Coordinator
- Submit proposal (lines 401-406) - Producer creates artifact and requests critics
- Critics evaluate (lines 414-476) - Each critic scores, identifies strengths/weaknesses
- Aggregate votes (lines 483) - Coordinator collects and aggregates votes
- Apply policy (lines 285-334) - Check thresholds, determine outcome
- Store decision (lines 281) - Record decision with full audit trail
Decision Policy¶
| Outcome | Condition |
|---|---|
| APPROVED | weighted_mean >= 7.0 AND all critics passed |
| REVISION_REQUESTED | weighted_mean < 5.0 OR any critic failed |
| ESCALATED_TO_HITL | std_dev > 2.0 (high disagreement) |
Key Code Sections¶
# Producer submits (lines 89-99)
proposal = NegotiationProposal(
artifact_type=ArtifactType.CODE,
artifact_id=artifact_id,
producer_id=self.agent_id,
artifact=json.dumps(artifact_content).encode(),
requested_critics=critics,
)
self.room_client.submit_proposal(proposal)
# Critic votes (lines 172-184)
vote = NegotiationVote(
artifact_id=artifact_id,
critic_id=self.agent_id,
score=7.5,
confidence=0.78,
passed=True,
strengths=["..."],
weaknesses=["..."],
)
self.room_client.submit_vote(vote)
# Coordinator decides (lines 249-283)
votes = self.room_client.get_votes(artifact_id)
aggregated = aggregate_votes(votes)
outcome, reason = self._apply_policy(votes, aggregated)
Scenarios Demonstrated¶
- Revision Requested - Security critic fails the artifact
- Approved - All critics pass with high scores
- HITL Escalation - Critics strongly disagree (high std_dev)
Handoff Example Walkthrough¶
File: sdks/py_sdk/examples/handoff_example.py
Concepts: Agent-to-agent handoff, capability matching, context transfer
Prerequisites¶
- SDK installed
- No services required (standalone demo)
Key Flow¶
- Source agent determines it needs specialist help
- Creates
HandoffRequestwith context and required capabilities - Target agent evaluates request via
on_handoff_request() - If accepted, target receives context via
on_handoff_received() - Source agent transitions or completes
Workflow Orchestration Walkthrough¶
File: sdks/py_sdk/examples/workflow_orchestration_example.py
Concepts: DAG workflows, dependency management, parallel execution
Key Components¶
WorkflowDefinition- DAG of workflow nodesWorkflowEngine- Validates DAG, detects cycles, executesWorkflowNode- Individual task with dependencies
Common Issues¶
| Issue | Solution |
|---|---|
CycleDetectedError | Remove circular dependencies in workflow |
| Node not executing | Check depends_on list is correct |
HITL Escalation Walkthrough¶
File: sdks/py_sdk/examples/hitl_escalation_example.py
Concepts: Human-in-the-loop, escalation triggers, decision waiting
Escalation Triggers¶
| Reason Type | When Used |
|---|---|
CONFLICT | Agents cannot reach consensus |
SECURITY_APPROVAL | Action requires security review |
TASK_ESCALATION | Complexity exceeds agent capability |
DEBATE_DEADLOCK | Negotiation cannot resolve |
4.7. Example READMEs¶
Each SDK has comprehensive README files in the examples directory:
These READMEs provide:
- Detailed descriptions of each example
- Command-line arguments and environment variables
- Prerequisites and service requirements
- Troubleshooting tips
- Feature matrices showing SDK component usage
4.7. Reference Services¶
Many examples require SW4RM reference services to be running. See the reference services documentation:
Quick start: