Skip to main content

Ollama Token Monitor 🚀

Production-ready Ollama token usage monitoring with automatic conversation splitting at configurable thresholds.

🌟 Features

  • Real-time token tracking - Monitor usage from Ollama API responses
  • Auto-conversation splitting - Split at 95% threshold to avoid truncation
  • Context preservation - Preserve system prompts and state across splits
  • State persistence - Save/load from JSON files
  • Exponential backoff - Robust retry logic with jitter
  • Comprehensive logging - Structured logging with configurable levels
  • Thread-safe - RLock protection for all shared state
  • Multiple models - Support for multiple Ollama models
  • Config-driven - YAML/JSON configuration files
  • Performance profiling - Sliding window metrics and percentiles

📦 Installation

pip install ollama-token-monitor

Or from source:

pip install -e .

🚀 Quick Start

from ollama_token_monitor import OllamaTokenMonitor, TokenMonitorConfig

# Configure
config = TokenMonitorConfig(
    ollama_host="http://localhost:11434",
    threshold_percent=95.0,
    check_interval_ms=500,
    max_retries=5,
    log_level="DEBUG",
)

# Create monitor
monitor = OllamaTokenMonitor(config)

# Start monitoring
monitor._start_monitoring()

# Get usage
print(f"Token usage: {monitor.get_percent_used():.2f}%")

# Stop
monitor.stop()

📋 Configuration

YAML

models:
  - name: "DeepSeek-V4-Pro"
    model: "hf.co/mradermacher/DeepSeek-V4-Pro-Qwen3.5-9B-UNCENSORED-heretic-i1-GGUF:Q4_K_M"
    ollama_host: "http://localhost:11434"
    threshold_percent: 95.0
    context_window: 8192
    check_interval_ms: 500
    max_retries: 5
    base_delay_ms: 100
    smart_preservation: true
    enable_profiling: true
    cache_enabled: true

JSON

{
  "models": [
    {
      "name": "DeepSeek-V4-Pro",
      "model": "hf.co/mradermacher/DeepSeek-V4-Pro-Qwen3.5-9B-UNCENSORED-heretic-i1-GGUF:Q4_K_M",
      "ollama_host": "http://localhost:11434",
      "threshold_percent": 95.0
    }
  ]
}

📁 Project Structure

ollama_token_monitor/
├── __init__.py
├── monitor.py
├── integration.py
├── cache.py
├── profiler.py
├── config.py
├── models.py
├── setup.py
├── pyproject.toml
├── README.md
├── CHANGELOG.md
├── LICENSE
└── tests/
    ├── test_monitor.py
    ├── test_integration.py
    ├── test_cache.py
    └── test_config.py

🧪 Testing

pytest tests/ -v --cov=ollama_token_monitor

📚 Documentation

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a Pull Request

📜 License

MIT License - see LICENSE for details.

🌐 Support

Metadata

Release files for ollama-token-monitor 0.2.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 ollama-token-monitor 0.2.0
File Size Uploaded Provenance Actions
ollama_token_monitor-0.2.0.tar.gz 16.9 kB Without provenance Details for ollama_token_monitor-0.2.0.tar.gz

Release files / ollama_token_monitor-0.2.0.tar.gz

Download URL ollama_token_monitor-0.2.0.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
727900c355ab5fb0997a9eaec2091a25c37e92202baa99083ea9454eda4283b1
BLAKE2b-256 checksum
How to use checksums
c6615e44756411c96aa727f1b6ce590bd808ca9870aae3e9c1a799796b793362
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for ollama-token-monitor 0.2.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file No provenance

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