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

Function conversation_loop

examples/memory/basic.py:70–227  ·  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 last_response_id: Optional[str] = None
77 last_usage = None
78
79 print("🧠 Claude with Memory & Web Search - Interactive Session")
80 print("Commands:")
81 print(" /quit or /exit - Exit the session")
82 print(" /clear - Start fresh conversation")
83 print(" /memory_view - See all memory files")
84 print(" /memory_clear - Delete all memory")
85 print(" /debug - View conversation history and token usage")
86
87 print(f"\n🧹 Context Management")
88 print("=" * 60)
89
90 while True:
91 try:
92 user_input = input("\nYou: ").strip()
93 except (EOFError, KeyboardInterrupt):
94 print("\nGoodbye!")
95 break
96
97 if user_input.lower() in ["/quit", "/exit"]:
98 print("Goodbye!")
99 break
100 elif user_input.lower() == "/clear":
101 messages = []
102 print("Conversation cleared!")
103 continue
104 elif user_input.lower() == "/memory_view":
105 result = memory.execute(BetaMemoryTool20250818ViewCommand(command="view", path="/memories"))
106 print("\n📁 Memory contents:")
107 print(result)
108 continue
109 elif user_input.lower() == "/memory_clear":
110 result = memory.clear_all_memory()
111 print(f"🗑️ {result}")
112 continue
113 elif user_input.lower() == "/debug":
114 print("\n🔍 Conversation history:")
115
116 if last_response_id:
117 print(f"📌 Last response ID: {last_response_id}")
118
119 if last_usage:
120 usage = last_usage
121 input_tokens = usage.get("input_tokens", 0)
122 cached_tokens = usage.get("cache_read_input_tokens", 0)
123 uncached_tokens = input_tokens - cached_tokens
124
125 print(f"📊 Last API call tokens:")
126 print(f" Total input: {input_tokens:,} tokens")
127 print(f" Cached: {cached_tokens:,} tokens")

Callers 1

basic.pyFile · 0.85

Calls 13

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
appendMethod · 0.45
tool_runnerMethod · 0.45

Tested by

no test coverage detected