Module 2 Β· Lesson 3 β Run It: Your First Diagnosis
Read time: ~6 min | Hands-on Here is the complete agent. No framework. No abstraction. Pure Python and the Anthropic SDK. Every line does exactly one thing. You will understand all of it. --- simple_agent.py import re import json import anthropic client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from env # β Demo tools β def show_ospf_neighbors(device: str) -> str: data = { "R1": "Gi0/1: neighbor 10.0.0.2 State INIT Dead 34\n" "Gi0/2: neighbor 10.0.0.6 State FULL Dead 38", "R2": "Gi0/0: neighbor 10.0.0.1 State INIT Dead 31", } return data.get(device, f"Device {device} not found") def show_ospf_interface(device: str, interface: str) -> str: data = { ("R1", "Gi0/1"): "Area 0, Hello 10, Dead 40, MTU 1500, auth: none", ("R2", "Gi0/0"): "Area 1, Hello 10, Dead 40, MTU 1500, auth: none", } return data.get((device, interface), f"Interface {interface} not found on {device}") def ping_device(device: str, target: str) -> str: return f"{device} -> {target}: Success rate 0 percent (0/5)" TOOLS = { "show_ospf_neighbors": { "function": show_ospf_neighbors, "description": "Get OSPF neighbor table from a router", "params": {"device": "Device hostname (string)"}, }, "show_ospf_interface": { "function": show_ospf_interface, "description": "Get OSPF config for a specific interface", "params": {"device": "Device hostname", "interface": "Interface name"}, }, "ping_device": { "function": ping_device, "description": "Ping a target IP from a source device", "params": {"device": "Source device hostname", "target": "Target IP address"}, }, } # ββ System prompt βββ SYSTEM_PROMPT = """You are a network troubleshooting agent for an MSP. For every problem, follow this exact format: Thought: [your reasoning about what to check next] Action: [tool_name] Params: {"param": "value"} OR when you have a diagnosis: Final Answer: [root cause and recommended fix] Available tools: {tools_description} Rules: - Always start with a Thought.