Home / Function/ conversation_loop() — anthropic-sdk-python Function Reference

conversation_loop() — anthropic-sdk-python Function Reference

Architecture documentation for the conversation_loop() function in basic.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  39d72171_4d60_dd21_ca18_ab93fe5bdef3["conversation_loop()"]
  ffd4efa5_68eb_4c4c_51fe_26bc27478a2b["basic.py"]
  39d72171_4d60_dd21_ca18_ab93fe5bdef3 -->|defined in| ffd4efa5_68eb_4c4c_51fe_26bc27478a2b
  35d60adf_2ecd_644b_1c44_7b4b135ea856["clear_all_memory()"]
  39d72171_4d60_dd21_ca18_ab93fe5bdef3 -->|calls| 35d60adf_2ecd_644b_1c44_7b4b135ea856
  461c4618_d95e_0141_da87_21efbe67e66c["start()"]
  39d72171_4d60_dd21_ca18_ab93fe5bdef3 -->|calls| 461c4618_d95e_0141_da87_21efbe67e66c
  019f4602_d268_5464_79eb_9336c8fc01c2["stop()"]
  39d72171_4d60_dd21_ca18_ab93fe5bdef3 -->|calls| 019f4602_d268_5464_79eb_9336c8fc01c2
  style 39d72171_4d60_dd21_ca18_ab93fe5bdef3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

examples/memory/basic.py lines 219–388

def conversation_loop():
    client = Anthropic()
    memory = LocalFilesystemMemoryTool()

    messages: list[BetaMessageParam] = []

    # Initialize tracking for debug
    last_response_id: Optional[str] = None
    last_usage = None

    print("🧠 Claude with Memory & Web Search - Interactive Session")
    print("Commands:")
    print("  /quit or /exit - Exit the session")
    print("  /clear - Start fresh conversation")
    print("  /memory_view - See all memory files")
    print("  /memory_clear - Delete all memory")
    print("  /debug - View conversation history and token usage")

    # Display context management settings
    print(f"\n🧹 Context Management")
    print("=" * 60)

    while True:
        try:
            user_input = input("\nYou: ").strip()
        except (EOFError, KeyboardInterrupt):
            print("\nGoodbye!")
            break

        if user_input.lower() in ["/quit", "/exit"]:
            print("Goodbye!")
            break
        elif user_input.lower() == "/clear":
            messages = []
            print("Conversation cleared!")
            continue
        elif user_input.lower() == "/memory_view":
            result = memory.execute(BetaMemoryTool20250818ViewCommand(command="view", path="/memories"))
            print("\n📁 Memory contents:")
            print(result)
            continue
        elif user_input.lower() == "/memory_clear":
            result = memory.clear_all_memory()
            print(f"🗑️ {result}")
            continue
        elif user_input.lower() == "/debug":
            print("\n🔍 Conversation history:")

            # Show last response ID if available
            if last_response_id:
                print(f"📌 Last response ID: {last_response_id}")

            # Show token usage if available
            if last_usage:
                usage = last_usage
                input_tokens = usage.get("input_tokens", 0)
                cached_tokens = usage.get("cache_read_input_tokens", 0)
                uncached_tokens = input_tokens - cached_tokens

                print(f"📊 Last API call tokens:")
                print(f"   Total input: {input_tokens:,} tokens")
                print(f"   Cached: {cached_tokens:,} tokens")
                print(f"   Uncached: {uncached_tokens:,} tokens")

                threshold = CONTEXT_MANAGEMENT["edits"][0]["trigger"]["value"]  # type: ignore
                print(f"   Context clearing threshold: {threshold:,} tokens")
                if input_tokens >= threshold:
                    print(f"   🧹 Context clearing should trigger soon!")
                elif input_tokens >= threshold * 0.8:  # 80% of threshold #type: ignore
                    print(f"   ⚠️  Approaching context clearing threshold!")

            print("=" * 80)

            for i, message in enumerate(messages):
                role = message["role"].upper()
                print(f"\n[{i + 1}] {role}:")
                print("-" * 40)

                content = message["content"]
                if isinstance(content, str):
                    print(content[:500] + "..." if len(content) > 500 else content)

Subdomains

Frequently Asked Questions

What does conversation_loop() do?
conversation_loop() is a function in the anthropic-sdk-python codebase, defined in examples/memory/basic.py.
Where is conversation_loop() defined?
conversation_loop() is defined in examples/memory/basic.py at line 219.
What does conversation_loop() call?
conversation_loop() calls 3 function(s): clear_all_memory, start, stop.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free