Okay, let's break down the cost of inferring a collection of 10,000 documents using paperless-ai, specifically focusing on the interaction with OpenRouter (since that's where the model costs are determined).
Understanding the Variables
The cost will depend on several factors:
Average Document Size: This is critical. If each document is a single page of text (~500 words, ~750 tokens), the cost will be far lower than if each document is a 50-page report (~37,500 tokens).
What you're inferring: Are you extracting only names, dates, and addresses? Or are you summarizing each document? The more complex the task, the more tokens the output will be.
The Model you choose: As we discussed, models like GPT-4o Mini, Gemini 1.5 Flash, and Claude 3 Haiku are cost-effective for large context. Cheaper models may not be able to handle the task well.
Input vs. Output Tokens: Remember that OpenRouter charges separately for input tokens (the documents you're feeding in) and output tokens (the model's results). The ratio of input to output tokens will influence the total cost.
Paperless-AI's Operations: Paperless-AI itself may have processing costs that are separate from the LLM inference costs. This should be minimal, though (mainly compute).
Error rates: You likely have a small percentage of documents that fail processing and/or produce poor results. These failures cost money and time, and it is worth having the best models available so that these errors are minimised.
Estimating the Costs (Example Scenario)
Let's make some assumptions for a scenario:
Average Document Size: 2 pages of text (approx. 1,500 tokens per document)
Inference Task: Summarizing each document and extracting key entities (a moderate task).
Output Token Ratio: Let's estimate that the output (summary + entities) is roughly 20% of the input (300 output tokens per document).
Model: OpenAI: GPT-4o Mini, which we found to be a great cost-capability tradeoff.
OpenRouter Cost using GPT-4o Mini (0.15/0.60 per 1M tokens):
Input Cost: (15 Million tokens / 1,000,000) * 0.15=2.25
Output Cost: (3 Million tokens / 1,000,000) * 0.60=1.80
Total OpenRouter Cost: 2.25+1.80 = $4.05
Error rate
With modern models, you can often get away with <2% error rate
However, the cost of errors is high, so higher-quality models will lead to better results.
Estimated Total Cost (Scenario)
Based on these assumptions, the estimated cost to infer your 10,000 documents using OpenRouter + GPT-4o Mini would be approximately $4.05. This is a very rough estimate, though.
Important Considerations and How to Get a More Accurate Estimate:
Run a Test Batch: The best way to get a precise estimate is to run a small batch of, say, 10-20 documents through Paperless-AI using your chosen model and settings. Monitor the token usage reported by OpenRouter (or Paperless-AI, if it provides that data). Extrapolate that cost to the full 10,000 documents.
Analyze your Documents: Take a close look at a representative sample of your documents to get a better handle on the average token count per document. Tools like tiktoken (the OpenAI tokenizer) can help you calculate token counts accurately.
Experiment with Prompts: Optimize the prompt you're sending to the LLM to get the desired results with the fewest output tokens possible. Clear, concise prompts can significantly reduce output token usage.
Monitor OpenRouter's Prices: Keep an eye on OpenRouter's model pricing, as it can change.
Consider a "Warm-up" phase: If you are using a new LLM for the first time, consider running the experiment on a small subset of the data to validate your approach and to get a sense of how you might adapt your prompt.
Example Python Code for Token Counting (using tiktoken):
python
import tiktoken
def count_tokens(text, model_name="gpt-4o"): # Choose the appropriate model name
"""Counts the number of tokens in a text string using a specific OpenAI model's tokenizer."""
try:
encoding = tiktoken.encoding_for_model(model_name)
num_tokens = len(encoding.encode(text))
return num_tokens
except KeyError:
print(f"Model {model_name} not found. Using cl100k_base (common fallback).")
encoding = tiktoken.get_encoding("cl100k_base") # Most models are using this now
num_tokens = len(encoding.encode(text))
return num_tokens
# Example usage
document_text = "This is a sample document with some text in it. This document is used as an example. It contains information about various things."
token_count = count_tokens(document_text)
print(f"The document contains approximately {token_count} tokens.")
In summary:
While the $4.05 estimate is plausible under the assumptions, the actual cost could be significantly higher or lower depending on your specific documents, the complexity of your task, the model you choose, and the efficiency of your prompts. Testing with a representative batch is the best way to get a reliable cost estimate. And remember to account for potential errors and the cost of those errors.