API usage on a per request basis

Open
#146 0 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript
Domain
api

Research direction

Start with the TypeScript client.general.partition call and rawResponse access shown in the issue; no repository files or tests are named. Determine the client and API changes needed to expose the actual strategy and billed page count without changing the response body. Done means callers can reliably read both values for each request.

Written by the indexing model from the issue text.

Description

Context

I have a multi-tenant SaaS application where I need to keep track of the Unstructured quota each tenant consumes.
Is there a way to track the amount of billed pages on a per request basis, the same way LLMs return the consumed tokens in each API request?
If not, replicating the same logic Unstructured uses to count pages for each different file type may be an impediment for us to continue using Unstructured, as we won't be able to track the tenant consumption in an appropriate manner.

The problem

Page count

Unstructured supports a wide range of different file types. It is not easy to replicate the page count for each file type: pdf, pptx, docx etc. The result is that we don't know the actual page count we are being billed for on a per request basis.

Strategy

Unstructured supports the auto strategy as an input in the API. However, the actual used strategy depends on the contents of the file. It is not trivial to replicate the same logic Unstructured uses internally to choose the actual strategy that will be used to partition the file. The result is that we don't know the actual strategy we are being billed for on a per request basis.

Arguments

Cost tracking is a real problem. So real that there are products in the market that focus on providing insights about the usage of different services (e.g. Helicone, Keywords AI, Sentry etc).

Example

The Unstructured API could add headers to provide more information about the request, keeping it backwards compatible without modifying the response body used today (a JSON array containing the partition elements).

const client = new UnstructuredClient({
    serverURL: process.env.UNSTRUCTURED_BASE_URL,
    security: {
      apiKeyAuth: process.env.UNSTRUCTURED_API_KEY
    }
  })

const response = await client.general.partition({
  partitionParameters: {
    files: {
      content: fileBuffer,
      fileName: fileName
    },
    strategy: 'auto'
  }
})

// Is it possible to update the Unstructured API to return this information? 🤔 
const actualUsedStrategy = response.rawResponse.headers.get('x-unstructured-strategy')
const actualPagesConsumed = parseInt(response.rawResponse.headers.get('x-unstructured-pages'))

Benchmarks

Many providers that support the OpenAI SDK provide transparency about the API consumption and pricing.

const openai = new OpenAI()

const response = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hi' }]
})

// We can calculate the actual cost of the request 🥳 
const actualModelUsed = response.model
const promptTokensUsage = result.usage?.prompt_tokens ?? 0
const completionTokensUsage = result.usage?.completion_tokens ?? 0

OpenAI has different prices for different gpt-4o snapshots.
When we send the model as gpt-4o, it is an alias for a specific snapshot that we are billed for.
This way, we can keep track of our usage in a very straightforward manner.

Image

Benefits

  • Transparency is increased, raising Unstructured to be on par of other AI providers regarding billing
  • Every Unstructured customer could benefit from this improvement, increasing its adoption as an important tool in the AI age
  • The API maintains its backwards compatibility
Dominant language
TypeScript
Stars
63
Forks
16
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Unstructured-IO/unstructured-js-client

All issues in Unstructured-IO/unstructured-js-client

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.