Signal · Methods

signal.count

signal.count(input) estimates the token count of a string or of a list of messages: one token per four characters, plus four tokens per message. The estimate is a heuristic with no tokenizer behind it, and no request is sent.

String
'Two for The Tempest on Saturday, please. Grace'
Messages
[
  {
    role: 'user',
    content: 'Two for The Tempest on Saturday, please. Grace',
  },
  {
    role: 'assistant',
    content: '{"show":"The Tempest","seats":2}',
  },
]
Estimated tokens
{ string: 12, messages: 28 }
Output matches the expected value

Runs in this page on the installed package. The input and the expected output are defined by the example.