()
| 68 | |
| 69 | |
| 70 | def 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") |
no test coverage detected