---
title: "Announcing pg_tiktoken: A Postgres Extension for Fast BPE Tokenization"
description: "pg_tiktoken is a Postgres extension on Neon that wraps OpenAI's tiktoken BPE tokenizer, exposing tiktoken_encode (returns token IDs) and tiktoken_count (returns token counts) for encodings such as cl100k_base, p50k_base, p50k_edit and r50k_base or their model names."
url: "https://swe.quest/articles/announcing-pg-tiktoken-a-postgres-extension-for-fast-bpe-tokenization-f53006f2756f2730"
canonical: "https://neon.com/blog/announcing-pg-tiktoken-a-postgres-extension-for-fast-bpe-tokenization"
source: "Neon"
published: "2023-03-14"
difficulty: "medium"
kind: "Announcement"
reading_minutes: 5
tags: ["postgres","llms","sql"]
---

# Announcing pg\_tiktoken: A Postgres Extension for Fast BPE Tokenization

A post on [Neon](https://swe.quest/sources/neon.md), published 2023-03-14, 5 min read, Announcement. Original: [https://neon.com/blog/announcing-pg-tiktoken-a-postgres-extension-for-fast-bpe-tokenization](https://neon.com/blog/announcing-pg-tiktoken-a-postgres-extension-for-fast-bpe-tokenization)

## Summary

pg\_tiktoken is a Postgres extension on Neon that wraps OpenAI's tiktoken BPE tokenizer, exposing tiktoken\_encode (returns token IDs) and tiktoken\_count (returns token counts) for encodings such as cl100k\_base, p50k\_base, p50k\_edit and r50k\_base or their model names. A practical pattern stores chat message history with a precomputed n\_tokens column, then uses a window-function cumulative sum ordered by recency to select the most recent messages that fit within a model's context budget (MAX\_HISTORY\_TOKENS = model max minus system and completion tokens) before calling gpt-3.5-turbo.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- SQL window functions ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- OpenAI chat API basics

## Keywords

pg\_tiktoken, tiktoken, Neon, Byte Pair Encoding, gpt-3.5-turbo, OpenAI

## Related posts

- [tokenizers v1: encode, decode and scaling, measured](https://swe.quest/articles/tokenizers-v1-encode-decode-and-scaling-measured-49d0c7f94c7739e1.md): Hugging Face · 2026-09-21 · Medium · 10 min read · Performance engineering, Rust, LLMs, Algorithms
  tokenizers v1 encodes text 3 to 30 times faster than v0.23 single-threaded on an Apple M4 Max and scales at 76% of linear across eight cores, while producing identical token IDs and keeping the same API.
- [BM25 is only as good as your tokens](https://swe.quest/articles/bm25-is-only-as-good-as-your-tokens-c687d331681bdece.md): Neon · 2026-10-07 · Medium · 8 min read · Search, PostgreSQL
  Lakebase Search's new lakebase\_tokenizer extension moves Postgres text-search synonym and stop-word dictionaries from server-side files into SQL tables, so managed and branchable Neon databases can define custom vocabularies with INSERT.
- [Tokenization in Transformers v5: Simpler, Clearer, and More Modular](https://swe.quest/articles/tokenization-in-transformers-v5-simpler-clearer-and-more-modular-71df3b1e7d817d35.md): Hugging Face · 2025-12-18 · Medium · 14 min read · LLMs, Python, Open source
  Transformers v5 redesigns tokenizers so that tokenizer architecture (normalizer, pre-tokenizer, model, post-processor, decoder) is separated from the trained vocabulary, much as PyTorch separates model architecture from weights.
- [Token Pooling-Aware Training for Multi-Vector Embedding Models](https://swe.quest/articles/token-pooling-aware-training-for-multi-vector-embedding-models-0e5234acf2e85460.md): Hugging Face · 2026-11-11 · Hard · 12 min read · Search, Model training, Machine learning, Python
  Pooling-aware training teaches a multi-vector (ColBERT-style) encoder to keep retrieval-relevant information when document token vectors are merged by Ward hierarchical clustering, improving quality at a fixed storage budget.
- [Building an AI-powered ChatBot using Vercel, OpenAI, and Postgres](https://swe.quest/articles/building-an-ai-powered-chatbot-using-vercel-openai-and-postgres-4fd423473384581f.md): Neon · 2023-03-09 · Medium · 13 min read · RAG & retrieval, PostgreSQL, Vector search, LLMs, Serverless
  A ChatGPT-style documentation assistant (Ask Neon) is built by storing OpenAI text-embedding-ada-002 vectors in Postgres with pgvector, retrieving context by cosine distance, and feeding it to text-davinci-003 from a Vercel Edge Function.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
