Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update examples/How_to_count_tokens_with_tiktoken.ipynb #2108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ChenyangLi4288 wants to merge 3 commits into openai:main
base: main
Choose a base branch
Loading
from ChenyangLi4288:main

Conversation

Copy link

@ChenyangLi4288 ChenyangLi4288 commented Sep 2, 2025

Summary

This PR adds comprehensive examples for counting tokens with structured responses to the existing "How to count tokens with tiktoken" notebook, addressing issue #2102. The addition includes a new function for calculating token overhead from structured response schemas, practical examples comparing different response formats, and API verification to ensure accuracy.

Motivation

The existing notebook provided excellent coverage for counting tokens in regular messages and tool calls, but lacked examples for structured responses - a key feature that adds token overhead due to schema definitions and validation instructions. This gap left developers without guidance on how to estimate token consumption when using structured outputs, which is increasingly important as more applications adopt structured response formats for better data consistency and parsing.

The new examples fill this gap by:

  • Providing a dedicated function to calculate structured response token overhead
  • Demonstrating both simple JSON object format and complex structured schemas
  • Showing how schema complexity directly impacts token consumption
  • Including API verification to validate the token counting estimates
  • Following the same educational pattern as existing sections for consistency

This addition makes the cookbook more complete and helps developers make informed decisions about when and how to use structured responses based on their token budget considerations.


For new content

When contributing new content, read through our contribution guidelines, and mark the following action items as completed:

  • I have added a new entry in registry.yaml (and, optionally, in authors.yaml) so that my content renders on the cookbook website.
  • I have conducted a self-review of my content based on the contribution guidelines:
    • Relevance: This content is related to building with OpenAI technologies and is useful to others.
    • Uniqueness: I have searched for related examples in the OpenAI Cookbook, and verified that my content offers new insights or unique information compared to existing documentation.
    • Spelling and Grammar: I have checked for spelling or grammatical mistakes.
    • Clarity: I have done a final read-through and verified that my submission is well-organized and easy to understand.
    • Correctness: The information I include is correct and all of my code executes successfully.
    • Completeness: I have explained everything fully, including all necessary references and citations.

We will rate each of these areas on a scale from 1 to 4, and will only accept contributions that score 3 or higher on all areas. Refer to our contribution guidelines for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /