def _compute_refill_amount(current_tokens, time_since_refill, limit, token_ms):
    """
    Compute the number of tokens accumulated since the last refill.

    Args:
      current_tokens (int): The number of tokens currently in the bucket.
      time_since_refill (int): Timestamp, in milliseconds, since the last time the bucket was refilled.
      limit (int): Time, in milliseconds, when the token retrieval started.
      token_ms (int): The rate of token accumulation per millisecond.

    Returns:
        int: The number of tokens accumulated since the last refill.
    """
    tokens = max(0, current_tokens)
    return min(limit - 1, tokens + int(token_ms * time_since_refill))
