Search documentation of Ragex

# Ragex.AI.Usage (Ragex v0.4.0)

View Source

Tracks AI provider usage including requests, tokens, and estimated costs.

Maintains statistics per provider with time-windowed tracking for rate limiting.

## Configuration

    config :ragex, :ai_limits,
      max_requests_per_minute: 60,
      max_requests_per_hour: 1000,
      max_tokens_per_day: 100_000

## Cost Estimation

Pricing as of January 2026 (subject to change):

- OpenAI GPT-4-turbo: $0.01/1K input, $0.03/1K output
- OpenAI GPT-3.5-turbo: $0.0005/1K input, $0.0015/1K output
- Anthropic Claude-3-Opus: $0.015/1K input, $0.075/1K output
- Anthropic Claude-3-Sonnet: $0.003/1K input, $0.015/1K output
- Anthropic Claude-3-Haiku: $0.00025/1K input, $0.00125/1K output
- DeepSeek R1: $0.001/1K input, $0.002/1K output
- Ollama: Free (local)

## Summary

### Functions

check_rate_limit(provider)

Check if rate limits would be exceeded by a new request.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

get_stats(provider \\ :all)

Get usage statistics for a provider or all providers.

record_request(provider, model, prompt_tokens, completion_tokens)

Record a request with token usage.

reset_stats()

Reset all usage statistics.

start_link(opts \\ [])

## Functions

# check_rate_limit(provider)

Check if rate limits would be exceeded by a new request.

Returns `:ok` if within limits, `{:error, reason}` if limit would be exceeded.

# child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# get_stats(provider \\ :all)

Get usage statistics for a provider or all providers.

# record_request(provider, model, prompt_tokens, completion_tokens)

Record a request with token usage.

# reset_stats()

Reset all usage statistics.

# start_link(opts \\ [])