MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / conversation_loop

Function conversation_loop

examples/memory/basic.py:70–239  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

68
69
70def conversation_loop():
71 client = Anthropic()
72 memory = BetaLocalFilesystemMemoryTool()
73
74 messages: list[BetaMessageParam] = []
75
76 # Initialize tracking for debug
77 last_response_id: Optional[str] = None
78 last_usage = None
79
80 print("🧠 Claude with Memory & Web Search - Interactive Session")
81 print("Commands:")
82 print(" /quit or /exit - Exit the session")
83 print(" /clear - Start fresh conversation")
84 print(" /memory_view - See all memory files")
85 print(" /memory_clear - Delete all memory")
86 print(" /debug - View conversation history and token usage")
87
88 # Display context management settings
89 print(f"\n🧹 Context Management")
90 print("=" * 60)
91
92 while True:
93 try:
94 user_input = input("\nYou: ").strip()
95 except (EOFError, KeyboardInterrupt):
96 print("\nGoodbye!")
97 break
98
99 if user_input.lower() in ["/quit", "/exit"]:
100 print("Goodbye!")
101 break
102 elif user_input.lower() == "/clear":
103 messages = []
104 print("Conversation cleared!")
105 continue
106 elif user_input.lower() == "/memory_view":
107 result = memory.execute(BetaMemoryTool20250818ViewCommand(command="view", path="/memories"))
108 print("\n📁 Memory contents:")
109 print(result)
110 continue
111 elif user_input.lower() == "/memory_clear":
112 result = memory.clear_all_memory()
113 print(f"🗑️ {result}")
114 continue
115 elif user_input.lower() == "/debug":
116 print("\n🔍 Conversation history:")
117
118 # Show last response ID if available
119 if last_response_id:
120 print(f"📌 Last response ID: {last_response_id}")
121
122 # Show token usage if available
123 if last_usage:
124 usage = last_usage
125 input_tokens = usage.get("input_tokens", 0)
126 cached_tokens = usage.get("cache_read_input_tokens", 0)
127 uncached_tokens = input_tokens - cached_tokens

Callers 1

basic.pyFile · 0.85

Calls 12

clear_all_memoryMethod · 0.95
startMethod · 0.95
stopMethod · 0.95
AnthropicClass · 0.90
SpinnerClass · 0.85
model_dumpMethod · 0.80
executeMethod · 0.45
getMethod · 0.45
tool_runnerMethod · 0.45

Tested by

no test coverage detected