# Local tokenizer and Codex helpers These commands work offline without API credentials. They do not save your text, edit settings, or install software. Use the editor to compare prompt wording and inspect token boundaries. Use `count` for a token total, or `inspect` when a script needs token IDs and exact bytes. You continue using the original text; saving token data is optional. The recovery guide below includes a failure-safe JSON export recipe. ## Explore tokens interactively ```sh openai tokenizer ``` The live editor normally opens below the shell command. In a direct Linux VS Code terminal, it uses a temporary screen when the shell screen is active. Earlier shell output returns when you exit, even after resizing the terminal. If the terminal cannot confirm which screen is active, the editor opens below the command. Type or paste text to see its exact token count. The editor supports up to 1 MiB of UTF-8 input. Use `tokenizer view` to open a command’s output or a file in the same editor. ```sh git diff | openai tokenizer view openai tokenizer view < draft.txt ``` The editor reads the complete UTF-8 input, including trailing newlines. When the producer finishes, the editor selects the first token. Page Down shows the next page. The terminal supplies the keyboard controls. Tab moves from Tokens to Text for editing. Empty input opens an empty draft. For output that another command can read, use `count` or `inspect`. Apple Terminal can draw brackets at both ends of the shell command. Choose **View > Hide Marks** to hide them. For one launch, submit the command with Shift+Command+Return. [Apple Terminal shortcuts](https://support.apple.com/guide/terminal/trmlshtcts/mac) Press Left or Right while typing to move the text cursor by one character. Tab moves directly between Text and Tokens. In Tokens, Left and Right select a token. Enter opens its details. Shift+Tab opens View settings from either pane. Use Left or Right there to choose Text, Token IDs, or Bytes. Outside the text field, `v` opens View, `m` opens Model, and `?` opens Help. When tokens are available, `d` opens Details for the selected token. In a chooser, use Up or Down to highlight a choice. Enter applies it, and Escape cancels it. Ctrl+C closes the panel and returns to the shell. Enter inserts a newline while editing text. Down opens options from any column on the last line. On earlier lines, Down moves the text cursor. Up moves to the previous text line. Home and End move to the current line's boundaries. Ctrl+Home and Ctrl+End jump to the beginning and end of the complete draft. Page Up and Page Down move by one visible text page and keep focus in Text. Wide terminals count wrapped text rows when paging. Ctrl+Left and Ctrl+Right move between words separated by whitespace. When lines exceed the visible text area, the Text label shows the current line and total lines. The editor keeps the complete draft even when only part is visible. The highlighted token follows the text cursor without recalculating tokens. The selected token and its byte count share a blue background. Monochrome terminals use reverse video. In Tokens, the text pane follows the selection and highlights its location. A selected line break appears as `↵`. If a token covers part of a displayed character, the text pane highlights the whole character. In Tokens, Up and Down select the token nearest the same column on the adjacent row. Tab places the text cursor at the start of the inspected token. If the token starts inside a displayed character, the cursor lands before that character. In the token strip, `␣` marks a selected space-only token. Its caption gives the exact number of spaces. The text editor shows a thin blinking cursor at the insertion point. At a token boundary, the highlight selects the token on the right. At the end, it selects the last token. Tokens can split a Unicode character; cursor movement still follows complete graphemes. Chooser focus uses the image picker's bold highlight. A checkmark identifies the applied choice. The main screen shows model families instead of encoding identifiers. It shows the token count and, for multiple tokens, the selected position. Colored token backgrounds reveal boundaries and stay consistent across Text, Token IDs, and Bytes. Text joins printable pieces without decorative brackets, quotes, or extra gaps. Literal punctuation remains unchanged. Control and nonprinting fragments retain visible escapes; partial UTF-8 uses hexadecimal bytes. An arrow beside the token position identifies keyboard focus in the results. The Bytes view adds the input byte count; full hexadecimal details remain available through Enter. Details quotes text to make leading spaces and escaped characters visible. Its subtitle shows the selected model family and its Default or Legacy label. In Details, Token ID identifies the token in the selected encoding. Starts tells you how many input bytes precede the token. Size tells you how many UTF-8 bytes the token contains. For example, the default encoding assigns ID 220 to a single space. That space uses one byte, hexadecimal `20`. Exact encoding names remain available in script output and `tokenizer encodings`. Scrolling controls appear when the complete details exceed the visible panel. With Tokens focused, Page Down selects the first token beyond the visible page. Page Up shows the preceding tokens. Home and End select the first and last token. Each page holds as many tokens as the current view and terminal size allow. In Help and Details, the same page keys scroll the visible rows. On a Mac laptop, Fn+Up sends Page Up and Fn+Down sends Page Down. [Mac keyboard shortcuts](https://support.apple.com/en-us/102650) Text and Token IDs are the primary reading views. Bytes supports exact-byte inspection and debugging. Ctrl+U removes text before the cursor, matching the image prompt editor. Paste preserves whitespace, line endings, and Unicode normalization. The editor escapes control characters for display. After Escape returns to editing, literal `[200~` remains ordinary text. Unfinished terminal control sequences above 1 MiB stop the editor with an input error. This terminal-protocol limit does not change accepted paste, file, or API payload limits. The Bytes view exposes token fragments that split a Unicode character. Editing clears stale results immediately. The editor cancels superseded computations and displays only the current revision. Long unbroken text can take time; editing and quitting remain available during computation. Terminal output runs independently, so a blocked display does not stop keyboard handling. The editor handles SIGINT, SIGTERM, and SIGHUP through cleanup. Successful signal cleanup returns 130, 143, and 129 respectively. Cleanup attempts terminal restoration and stops owned helpers. Restoration failures remain errors, including when a terminal becomes inaccessible. If computation fails, edit the text or press `r` while Tokens has focus. The draft remains available until you exit; the CLI never saves it. The editor requires terminal input and output, automatic format, and at least 40 columns by 12 rows. Bare `openai tokenizer` prints guidance for pipes, CI, `TERM=dumb`, and explicit text format without consuming stdin. `tokenizer view` requires interactive terminal output in automatic mode. `NO_COLOR` retains keyboard controls and visible focus markers. It retains the monochrome cursor and selected-token styling while suppressing token colors. Use `count` or `inspect` for JSON output and scripts. ## Count and inspect tokens ```sh openai tokenizer count --text "Hello, world!" openai tokenizer inspect --text "Hi!" --encoding cl100k_base openai tokenizer count --text " return value" --encoding p50k_base openai tokenizer count --file prompt.txt printf 'Hello, world!' | openai tokenizer count printf 'Hello, world!' | openai tokenizer count -n openai --format json tokenizer inspect --file prompt.txt openai tokenizer encodings openai tokenizer licenses ``` Choose `--text` or `--file`. Either option overrides piped stdin without reading it. Combining both options is an error. Without either option, the command reads redirected stdin through EOF. Use `--file -` to read interactive stdin through EOF. Plain terminal input without an explicit source returns guidance instead of waiting. These input rules apply to `count` and `inspect`, not the live editor. `--text` treats `@`, JSON, YAML, and special-token spellings as ordinary text. File paths are literal; `--file` does not expand `@` references. Empty input produces zero tokens. The tokenizer preserves whitespace, BOM, combining marks, and final newlines. It performs no Unicode normalization and rejects invalid UTF-8. The default encoding remains `o200k_base`. The tokenizer also supports `cl100k_base`, `r50k_base`, and `p50k_base` offline. `o200k_base` supports GPT-5.x, o1/o3, o4-mini, GPT-4o, GPT-4.1, and GPT-4.5. `cl100k_base` supports original GPT-4, GPT-4 Turbo, and GPT-3.5. The legacy `r50k_base` encoding supports GPT-3 models such as `davinci`. The legacy `p50k_base` encoding supports original Codex models and `text-davinci-002`/`text-davinci-003`. These model families follow the [OpenAI tokenizer reference](https://developers.openai.com/cookbook/examples/how_to_count_tokens_with_tiktoken). The original Codex models differ from the current Codex CLI. Use exact encoding names. The command does not guess an encoding from a model name. Token IDs follow OpenAI tiktoken 0.14.0 with fixed Unicode 16 character rules. Compiler upgrades do not change those rules. Legacy encodings preserve their case-sensitive contractions and distinct whitespace tokens. The input limit is 1 MiB. Long unbroken text can take minutes; Ctrl+C stops the process. The tokenizer processes the complete input because arbitrary chunking can change token boundaries. This local limit does not change API request or response limits. Embedded callers can cancel blocked Unix pipe reads through the command context. The tokenizer requires exclusive consumption of that pipe and preserves its ownership, flags, and deadlines. Custom readers can implement `ReadContext(context.Context, []byte) (int, error)` for cooperative cancellation. Other readers, including Windows file reads, check cancellation between synchronous reads. Default count output: ```text Encoding: o200k_base Input bytes: 13 Tokens: 4 ``` Inspection includes each token ID and its half-open byte range `[start_byte, end_byte)`. `bytes_hex` preserves each token's exact bytes. JSON includes `text` only when the token fragment is valid UTF-8. A token can split a Unicode character; concatenate its bytes to reconstruct the input. Readable output quotes fragments and escapes terminal controls. Counts describe plain text under the selected encoding. They are not complete request counts, billed usage, prices, or context-limit guarantees. Messages, tools, images, and files can add tokens. The existing authenticated command remains available for server request counting: ```sh openai responses input-tokens count --model YOUR_MODEL --input "Hello" ``` See the [official token-counting guide](https://developers.openai.com/api/docs/guides/token-counting). ## Codex instructions and destinations ```sh openai codex # print installation and configuration instructions openai --format json codex # structured instructions openai codex --destination config # print a fixed documentation URL openai codex --destination docs --open # explicitly request a browser launch ``` The OpenAI CLI command `openai codex` prints instructions for the separate Codex CLI executable, `codex`. Automatic terminal output groups installation, startup, configuration, and official links in the CLI theme. Explicit text and JSON formats retain their plain output contracts. It never runs installation commands, launches an agent, logs in, or changes configuration. Codex CLI and destination services handle their own sign-in and access checks. | Destination | Purpose | | --- | --- | | `docs` | Codex CLI installation and usage documentation. | | `config` | Codex configuration documentation. | | `app` | Desktop app documentation and download instructions. | | `web` | ChatGPT web, where supported cloud workflows are available. | Default and piped output never open a browser. `--open` requires an explicit destination and accepts no arbitrary URL. The command prints the URL before requesting a browser launch. The system browser launcher receives only named desktop, profile, and locale settings. Linux retains the `BROWSER` fallback supported by `xdg-open`. If opening fails, the URL remains available and the command exits nonzero. Instructions and destinations were verified on October 8, 2026. Check [Codex CLI documentation](https://learn.chatgpt.com/docs/cli) for later installation changes. See [configuration basics](https://learn.chatgpt.com/docs/config-file/config-basic) for configuration details. ## Formats, failures, and dependencies Tokenizer subcommands and Codex support `--format auto`, `text`, and `json`. Bare `openai tokenizer` requires `count` or `inspect` for JSON output. `auto` produces readable text through pipes by default. `tokenizer count --number` (or `-n`) writes only the token count followed by a newline. The input `Hello, world!` produces `4`. You can also select the count with `--transform token_count`. Use `--transform input_bytes` to select the byte count. Use `--transform encoding --raw-output` to print the encoding name without JSON quotes. Extraction follows the shared CLI behavior. A missing path returns the complete count object. For `inspect`, use `--format json` to pipe the complete token records to a JSON reader. The existing `--format-error` behavior remains available. Invalid input, read failures, opening failures, and output failures return nonzero statuses. Interrupted output can be incomplete. ## Recover from a failure Check the exit status before using output in a script. Do not treat partial JSON or a printed Codex URL as proof that the command succeeded. Script commands never retry, change your input, install a browser, or choose a different encoding automatically. The live editor replaces tokenization work when you edit text or select another encoding. | Failure | What remains | What to do next | | --- | --- | --- | | Live preview fails | Your draft remains; stale results disappear. | Edit the text or press `r` with results selected. Use `tokenizer inspect` for plain output. | | Invalid or oversized paste | The editor keeps the complete previous draft. | Paste valid UTF-8 within the 1 MiB total limit. The editor never truncates accepted input. | | Terminal becomes too small | The draft and current results remain in memory. | Resize to at least 40 columns by 12 rows, or press Ctrl+C. | | Editor startup or display fails | The CLI attempts terminal restoration and exits nonzero. | Use `tokenizer count` or `tokenizer inspect` with an explicit source. | | Terminal stops accepting output | Cancellation still restores native terminal modes. Final display cleanup may not reach the terminal. | Press Ctrl+C to exit, then restore terminal output before continuing. | | Editor exits or receives a termination signal | The draft is discarded; no files or settings change. | Keep important source text in your own file before opening the editor. | | No input in a terminal | No token result. | Supply `--text`, `--file`, or a pipe. Use `--file -` for interactive stdin. | | Conflicting or repeated input options | No token result; input stays unread. | Supply each option once and choose either `--text` or `--file`. | | Unknown encoding or option | No token result. | Run `openai tokenizer encodings` or command `--help`. Use an encoding name, not a model name. | | Invalid UTF-8 | No token result; source bytes remain unchanged. | Convert a copy from its known source encoding to UTF-8. Do not discard invalid bytes silently. | | Input exceeds 1 MiB | No token result; source bytes remain unchanged. | Choose a smaller complete input. Counts from arbitrary chunks may differ from the complete text. | | File cannot open or read | No token result. | Check the supplied path, file type, permissions, and producer. The error does not print private paths. | | Input producer has not closed stdin | The command waits for EOF. | Finish the producer or send EOF. Press Ctrl+C to stop. | | Producer fails after writing a valid prefix | The tokenizer can successfully count that prefix. | Check the producer's exit status too. Bash and zsh can use `set -o pipefail`. | | Unsupported output format or transformation | No result. | Use `--format text` or `--format json`. Only `tokenizer count` accepts `--transform` and `--raw-output` among these local commands. | | Output file or pipe fails | Previously written bytes can remain incomplete. | Fix the output destination and rerun. Discard partial output; do not append a retry to it. | | Tokenization is interrupted | No complete result is promised. | Rerun only if you still need the result. Long unbroken input can remain slow. | | Missing or unknown Codex destination | No browser launches. | Choose `docs`, `config`, `app`, or `web`. `--open` requires an explicit destination. | | Browser launcher is missing, fails, or exceeds ten seconds | The printed URL remains; the command exits nonzero. | Check whether the page opened. Otherwise, open the printed URL manually. | An output failure prevents `--open` from launching a browser. A browser launch can partially succeed before its launcher reports an error. Cancellation terminates the launcher; it does not close an already launched browser. The CLI cannot verify page loading, network access, or destination sign-in. If stderr also fails, an explanatory message might be unavailable; the exit status still indicates failure. The tokenizer cannot distinguish normal EOF from a producer that failed after writing data. Without `pipefail`, a shell pipeline can hide the producer's failure behind the tokenizer's successful exit. For file output, write to a temporary file and replace the final file only after success: ```sh if [ -d tokens.json ]; then printf '%s\n' 'tokens.json is a directory; choose an output file.' >&2 exit 1 fi token_result=$(mktemp './tokens.json.XXXXXX') || exit 1 trap 'rm -f "$token_result"' EXIT if openai --format json tokenizer inspect --file prompt.txt >"$token_result"; then mv "$token_result" tokens.json else token_status=$? printf '%s\n' 'Tokenization failed; existing tokens.json was kept.' >&2 exit "$token_status" fi ``` Shell redirection alone can truncate an existing file before the CLI starts. The temporary-file example preserves an existing result when tokenization fails. ## Tokenizer dependencies The tokenizer reads embedded OpenAI vocabularies through `github.com/tiktoken-go/tokenizer`. It uses `github.com/pkoukk/tiktoken-go v0.1.8` for ordinary BPE encoding with fixed Unicode 16 rules. Each encoding initializes once, and both counting and inspection validate every returned byte against the input. The CLI never downloads vocabulary data during execution. Dependency and Unicode updates must retain reference checks across supported Go versions. `openai tokenizer licenses` prints the complete bundled notices.