Optimizing for Cost

Optimizing for Cost#

ContextGem offers several strategies to optimize for cost efficiency while maintaining extraction quality:

  • 💸 Select Cost-Efficient Models: Use smaller/distilled non-reasoning LLMs for extracting aspects and basic concepts (e.g. titles, payment amounts, dates).

  • ⚙️ Use Default Parameters: All the extractions will be processed in as few LLM calls as possible.

  • 📉 Enable Justifications Only When Necessary: Do not use justifications for simple aspects or concepts. This will reduce the number of tokens generated.

  • 📊 Monitor Usage and Cost: Track LLM calls, token consumption, and cost to identify optimization opportunities.

Example of optimizing extraction for cost#
# Example of optimizing extraction for cost

import os

from contextgem import DocumentLLM, LLMPricing

llm = DocumentLLM(
    model="openai/gpt-4o-mini",
    api_key=os.environ.get("CONTEXTGEM_OPENAI_API_KEY"),
    pricing_details=LLMPricing(
        input_per_1m_tokens=0.150,
        output_per_1m_tokens=0.600,
    ),  # add pricing details to track costs
)

# ... use the LLM for extraction ...

# ... monitor usage and cost ...
usage = llm.get_usage()  # get the usage details, including tokens and calls' details.
cost = llm.get_cost()  # get the cost details, including input, output, and total costs.
print(usage)
print(cost)