Count tokens and control input length
Tokens are the units a model uses to process text. The number of tokens is not the same as the number of characters or words, and it depends on the selected model and tokenizer.
Count tokens directly
import akasha.helper as ah
text = "This is a short example for token counting."
tokens = ah.myTokenizer.compute_tokens(
text,
"openai:gpt-4o",
)
print(f"Token count: {tokens}")
Use the same model alias that you plan to use for the request. Different model families can produce different counts for the same text.
Control an ask request
ask, RAG, summary, and related APIs count input tokens internally. Use max_input_tokens to limit the input accepted by a request:
For Gemini, internal budgeting uses local estimates by default and makes no
remote count_tokens request. Document splitting and input limits still apply
to those estimates. Leave room below the model's context limit; the provider
validates the actual request. See myTokenizer
for explicit remote counting with exact=True.
import akasha
qa = akasha.ask(
model="gemini:gemini-2.5-flash",
max_input_tokens=3000,
)
answer = qa(
prompt="Summarize the important points.",
info="./docs",
)
print(answer)
When the prompt or context is too large, akasha may split or truncate document context, or raise an input-size error depending on the workflow.
See token information in logs
Enable logs or verbose output when diagnosing input size:
qa = akasha.ask(
model="openai:gpt-4o",
max_input_tokens=3000,
keep_logs=True,
verbose=True,
)
The logs can include prompt and document token information for the request.
Warning
A local token count is an estimate of the tokenizer calculation. It may differ from the Provider's final usage or billing count, especially when request metadata, system messages, or provider-specific formatting are included.