DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk5 min

How to Count Tokens and Split Text in a Node.js Summary API

A practical Node.js pipeline for counting request tokens, splitting long sales notes at meaningful boundaries, combining chunk summaries, and estimating API spend.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To summarize long sales notes without guessing at model limits or spend, assemble the exact request, count its tokens with the provider’s request-count endpoint when available, and split the text only if it exceeds a configured input budget. Summarize meaningful chunks, combine their notes when the overall account story matters, and record the API’s actual usage. Token counts and costs depend on the provider, model, and complete request—not just the source text.

How do I count tokens before sending a request?

Count the request you intend to send, including system instructions, user content, message structure, and any chunk-specific directions. Words or characters can provide only a rough estimate. OpenAI’s Help Center gives a heuristic of about four characters or three-quarters of an English word per token, but tokenization varies by text and model; it is not a substitute for counting the actual request.

As an Amazon Associate I earn from qualifying purchases.

OpenAI’s Responses input-token endpoint accepts the same input format as the Responses API, so it can count the assembled payload before generation. Its JavaScript SDK exposes this as client.responses.inputTokens.count; the result includes input_tokens. The endpoint can also count PDF file inputs based on processed input. See OpenAI’s token-counting guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Google Gemini and Anthropic also document provider-specific count endpoints. Their counts and supported payloads are not interchangeable: count with the same provider and request shape you plan to use. Google’s Gemini token guide documents count_tokens and a Node.js example; Anthropic’s token-counting documentation describes its message token-count endpoint.

Use the provider count for the assembled request

A local tokenizer can be useful for a quick plain-text preflight, but it may not account for images and files, tools, schemas, formatting, or model-specific behavior. Treat it as an estimate, not the final budget check. OpenAI explains these limits in its token-counting guide.

Build a budget that includes the response

A model’s context window is a total token budget, not a source-text allowance. Instructions, request structure, generated output, and—in some models—reasoning or other output allowances also use capacity. Choose a model-specific input budget below the available context limit, reserve room for the intended summary, and use a conservative margin appropriate to the application. The sources do not establish one universal margin. OpenAI describes context limits and possible truncation in its conversation-state guide.

How do I summarize text that is too long for the model?

Use a simple pipeline: construct the request, count it, send it when it fits, or split the source and summarize the resulting chunks. Recount after adding per-chunk instructions; those repeated instructions consume tokens too. Keep the model limits, output reserve, and safety margin in configuration so they can be adjusted when the selected model changes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

1. Assemble and count the exact request

With OpenAI’s JavaScript SDK, the generation pattern uses import OpenAI from "openai", creates a client, calls client.responses.create({ model, input }), and reads response.output_text. The official text-generation guide shows this pattern. The token-counting guide documents client.responses.inputTokens.count. Keep the same model and input format when counting and generating.

Compare the count with your configured input budget, which should already account for the output reserve and safety margin. If the request fits, send it as one request; chunking every long-looking transcript by default adds calls and can weaken relationships between distant passages.

2. Split only when needed, at meaningful boundaries

Prefer paragraph, message, or section boundaries over arbitrary character lengths. Preserve source identifiers and sequence numbers so you can trace each summary back to its place in the sales record and restore the original order. There is no evidence-based universal chunk size: select one from the chosen model’s limits, request overhead, and the behavior of your application, then validate it on representative material.

Keep qualifications attached to the facts they qualify. A commitment’s date, an objection’s condition, or a stated need can change meaning if separated from its surrounding sentence or speaker attribution. If a boundary would break that relationship, adjust the split rather than treating a fixed character count as more important than context.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

3. Summarize chunks for traceable facts

For sales material, ask chunk-level summaries to capture useful fields such as needs, objections, commitments, dates, and uncertainty. Include the chunk’s source identifier and sequence number with its notes. This makes the final synthesis easier to audit and helps distinguish an explicit commitment from a tentative statement or inference.

4. Combine chunk notes when the whole account matters

If the task needs an account-level conclusion, send the chunk notes to a final synthesis step and ask it to reconcile repeated or conflicting facts while preserving uncertainty. A two-stage summary is an engineering approach, not a guarantee that all facts survive compression. Test representative sales notes and transcripts for omissions, contradictions, and invented commitments. For a task that depends on relationships across distant passages, compare this approach against a single request when that request fits the context budget.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How much will this summary API call cost?

For a straightforward request, estimate token charges as (input tokens × current input rate) + (output tokens × current output rate). Apply cached-token rates or other pricing categories only when they actually apply. Check the selected model’s current rates on the provider’s OpenAI pricing page; its rates are model- and token-category-specific and can change, so do not bake a rate into evergreen code.

A lower input rate alone does not establish a lower total cost. Chunking can repeat instructions across calls, output length varies, and providers may tokenize the same sales text differently. Count and estimate with the provider and model you plan to use, then record actual usage from representative calls. Compare quality as well as spend: a shorter summary is not necessarily a better one. OpenAI’s Help Center guidance on tokens advises evaluating representative tasks rather than comparing only visible response length.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How should I choose a summarization API?

There is no established cheapest provider for an equivalent sales-summary workload. Compare a specific model and context tier against your own texts and requirements; a token price or count by itself cannot settle the choice.

  • Price: Check current input and output rates for the exact model and context tier, along with any applicable cached-token or other categories.
  • Counting coverage: Confirm that the provider’s count endpoint accepts the payload shape your application sends, including any files, tools, or structured inputs you use.
  • Limits and overflow behavior: Verify the model’s context and output limits and what happens when a request exceeds them.
  • Factual retention: Evaluate representative sales material for accurate needs, objections, dates, and commitments, as well as preserved uncertainty.
  • Operations: Verify account limits, latency, data handling, and regional availability for your own deployment; these vary and require current provider-specific checks.

OpenAI, Google Gemini, and Anthropic all document token-counting methods, but their counting behavior is provider-specific. Start with the API whose payload and operational requirements fit, then compare alternatives using the same representative workload and quality checks.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.