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 LLMs for extracting aspects and basic concepts (e.g. titles, payment amounts, dates).

  • ⚖️ Consider a Decision Model for Closed Questions: Boolean, label and rating concepts, as well as aspects, can be extracted with a decision model, which generates no text. (See Decision Models for when a decision model is a good fit, and for what its requests cost.)

  • ⚙️ Use Default Parameters: without concurrency, the aspects or concepts with the same settings are extracted in one call, i.e. in as few model calls as possible. With use_concurrency=True, each is extracted in a call of its own, which is sent the text again, unless max_items_per_call is set.

  • 📉 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 model 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


llm = DocumentLLM(
    model="openai/gpt-6-luna",
    api_key=os.environ.get("CONTEXTGEM_OPENAI_API_KEY"),
    auto_pricing=True,  # calculate the costs with `genai-prices`, to track them
    # or set `pricing_details=ModelPricing(...)` with the prices of your provider
)

# ... 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)

📨 What a Request Sends#

Each request to an LLM sends the system message, the instructions of the task with the aspects or concepts of the request, and the content:

  • The system message: the default one is written for extraction from documents, and you can replace it, or send none, with system_message (see Configuring LLM(s)).

  • The instructions: fixed text for the kind of request, with more of it for a reasoning model, and the descriptions of the aspects or concepts, with their structures, labels and examples.

  • The content: for concepts without references, the text of the document or of the aspect as it is. For aspects, and for concepts with references, each paragraph with its ID in markup, and its additional context, if any; with references to sentences, each sentence with its own ID. For a document of many short paragraphs, e.g. lines of a log, the markup is a noticeable part of the request: extract the concepts that need no references without them, as their text is sent as it is.

The system message and the instructions are sent again with each request: the more requests an extraction takes, e.g. with max_items_per_call or max_paragraphs_to_analyze_per_call (see Extraction Methods), the more often they are paid for. To see the requests, and count their tokens, before any is paid for, capture them with a stand-in for the LLM (see Testing Without API Calls). Count with the tokenizer of the model where you have it: for a model whose tokenizer litellm.token_counter does not have, e.g. a Claude model, it counts with tiktoken, i.e. its count is an estimate.