LangChain-powered intelligent agent for Token Bowl Chat servers
Project description
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 keyOPENROUTER_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 attemptsretry_base_delay: Initial retry delay in secondsmax_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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file token_bowl_chat_agent-1.0.0.tar.gz.
File metadata
- Download URL: token_bowl_chat_agent-1.0.0.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd769da844d8cd580d384a31f2dd88fdf72c81935952813647a05f3fad35b878
|
|
| MD5 |
ed0f83b3167d10f9e14039debfcfbaee
|
|
| BLAKE2b-256 |
eae782f7ed9c9ae03e3874bbec2ee3f8ef003ffbfe190426e53772a62aaa8f76
|
Provenance
The following attestation bundles were made for token_bowl_chat_agent-1.0.0.tar.gz:
Publisher:
publish.yml on RobSpectre/token-bowl-chat-agent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
token_bowl_chat_agent-1.0.0.tar.gz -
Subject digest:
dd769da844d8cd580d384a31f2dd88fdf72c81935952813647a05f3fad35b878 - Sigstore transparency entry: 725461784
- Sigstore integration time:
-
Permalink:
RobSpectre/token-bowl-chat-agent@6f848505ce89d549190e00b85cb7a4c5a1ebb40f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/RobSpectre
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6f848505ce89d549190e00b85cb7a4c5a1ebb40f -
Trigger Event:
release
-
Statement type:
File details
Details for the file token_bowl_chat_agent-1.0.0-py3-none-any.whl.
File metadata
- Download URL: token_bowl_chat_agent-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03f8e611946667258533db7a020292a250a33977228c383f02dde82ec4c6de25
|
|
| MD5 |
7393487a4ea2fd491e438ea178a0b182
|
|
| BLAKE2b-256 |
71643ebcaf051b4dcf438c39fb2c2b19de2b5300624af18cf69fb5abdcf3fb14
|
Provenance
The following attestation bundles were made for token_bowl_chat_agent-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on RobSpectre/token-bowl-chat-agent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
token_bowl_chat_agent-1.0.0-py3-none-any.whl -
Subject digest:
03f8e611946667258533db7a020292a250a33977228c383f02dde82ec4c6de25 - Sigstore transparency entry: 725461790
- Sigstore integration time:
-
Permalink:
RobSpectre/token-bowl-chat-agent@6f848505ce89d549190e00b85cb7a4c5a1ebb40f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/RobSpectre
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6f848505ce89d549190e00b85cb7a4c5a1ebb40f -
Trigger Event:
release
-
Statement type: