Skip to main content

Token Bowl Chat Agent

A LangChain-powered intelligent agent for Token Bowl Chat servers with WebSocket support.

Features

  • 🤖 Intelligent chat agent powered by LangChain and OpenRouter
  • 🔄 WebSocket connectivity with automatic reconnection
  • 🧠 Conversation memory management and context window optimization
  • 🛠️ Model Context Protocol (MCP) tool support
  • 📊 Comprehensive statistics tracking
  • 🔁 Automatic retry mechanism with exponential backoff
  • 🎯 Anti-repetition detection and global reset capability
  • ⏰ Rate limiting with cooldown periods
  • 📝 Support for direct messages and room conversations

Installation

pip install token-bowl-chat-agent

With MCP Support

pip install token-bowl-chat-agent[mcp]

Quick Start

As a Library

from token_bowl_chat_agent import TokenBowlAgent
import asyncio

async def main():
    agent = TokenBowlAgent(
        api_key="your-token-bowl-api-key",
        openrouter_api_key="your-openrouter-api-key",
        model_name="openai/gpt-4o-mini"
    )

    await agent.run()

asyncio.run(main())

Using System and User Prompts

agent = TokenBowlAgent(
    api_key="your-api-key",
    openrouter_api_key="your-openrouter-key",
    system_prompt="You are a helpful assistant specialized in Python programming",
    user_prompt="Please help users with their Python questions"
)

Configuration Options

agent = TokenBowlAgent(
    # Required
    api_key="your-token-bowl-api-key",
    openrouter_api_key="your-openrouter-api-key",

    # Optional - Model Configuration
    model_name="openai/gpt-4o-mini",  # Any OpenRouter model
    context_window=128000,  # Max context window in tokens

    # Optional - Prompts
    system_prompt="path/to/system_prompt.md",  # Path or text
    user_prompt="Respond to these messages",  # Path or text

    # Optional - Connection Settings
    server_url="wss://api.tokenbowl.ai",
    queue_interval=30.0,  # Seconds before processing queued messages
    max_reconnect_delay=300.0,  # Max seconds between reconnection attempts

    # Optional - Rate Limiting
    cooldown_messages=3,  # Messages before cooldown
    cooldown_minutes=10,  # Cooldown duration

    # Optional - Memory Management
    max_conversation_history=10,  # Messages to keep in memory

    # Optional - MCP Settings
    mcp_enabled=True,
    mcp_server_url="https://tokenbowl-mcp.haihai.ai/sse",

    # Optional - Advanced Settings
    similarity_threshold=0.85,  # Repetition detection threshold
    max_retry_attempts=3,
    retry_base_delay=5,
    max_retry_delay=60,
    verbose=False
)

Environment Variables

  • TOKEN_BOWL_CHAT_API_KEY: Your Token Bowl Chat API key
  • OPENROUTER_API_KEY: Your OpenRouter API key

Features in Detail

Automatic Message Queue Processing

The agent automatically queues incoming messages and processes them in batches based on the configured queue_interval. This helps manage API rate limits and provides more coherent responses.

Conversation Memory Management

The agent maintains a conversation history with automatic trimming to stay within token limits. You can configure the maximum number of messages to keep with max_conversation_history.

Rate Limiting and Cooldown

After sending a configured number of messages (cooldown_messages), the agent enters a cooldown period to prevent excessive API usage. During cooldown, messages are queued but not processed.

Retry Mechanism

Failed messages are automatically retried with exponential backoff. Configure retry behavior with:

  • max_retry_attempts: Maximum number of retry attempts
  • retry_base_delay: Initial retry delay in seconds
  • max_retry_delay: Maximum retry delay in seconds

Anti-Repetition System

The agent detects repetitive responses using similarity matching. If a response is too similar to recent messages (based on similarity_threshold), it performs a global reset to break out of loops.

MCP Tool Support

When MCP is enabled, the agent can use tools provided by the MCP server for enhanced functionality like web search, calculations, and more.

Statistics Tracking

The agent tracks comprehensive statistics including:

  • Messages received and sent
  • Token usage (input and output)
  • Errors and reconnections
  • Retry attempts and failures
  • Uptime and cooldown status

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Release files for token-bowl-chat-agent 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for token-bowl-chat-agent 1.0.0
File Size Uploaded
token_bowl_chat_agent-1.0.0.tar.gz 20.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for token-bowl-chat-agent 1.0.0
File Interpreter ABI Platform
token_bowl_chat_agent-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 38.4 kB

Release files / token_bowl_chat_agent-1.0.0.tar.gz

Download URL token_bowl_chat_agent-1.0.0.tar.gz
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
dd769da844d8cd580d384a31f2dd88fdf72c81935952813647a05f3fad35b878
BLAKE2b-256 checksum
How to use checksums
eae782f7ed9c9ae03e3874bbec2ee3f8ef003ffbfe190426e53772a62aaa8f76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2025.

Transparency log

Release files / token_bowl_chat_agent-1.0.0-py3-none-any.whl

Download URL token_bowl_chat_agent-1.0.0-py3-none-any.whl
Size 18.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03f8e611946667258533db7a020292a250a33977228c383f02dde82ec4c6de25
BLAKE2b-256 checksum
How to use checksums
71643ebcaf051b4dcf438c39fb2c2b19de2b5300624af18cf69fb5abdcf3fb14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page