You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" print(f'{response.usage.prompt_tokens} prompt tokens counted by the OpenAI API.')\n",
" print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 8. Counting tokens for chat completions with structured responses\n",
"\n",
"Structured responses allow you to define a JSON schema that the model must follow when generating responses. This feature adds additional tokens to account for the schema definition and validation instructions.\n",
"\n",
"Below is an example function for counting tokens for messages that include structured response schemas, passed to `gpt-3.5-turbo`, `gpt-4`, `gpt-4o` and `gpt-4o-mini`.\n"
" \"content\": \"You are a helpful assistant that extracts information from text and formats it as structured data.\"\n",
" },\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": \"Please extract information about John Smith. He is 35 years old, works as a software engineer, and his email is john.smith@example.com.\"\n",
" }\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compare token counts for different response formats\n",
"2. **Structured schema format** adds more significant overhead due to the schema definition and validation instructions\n",
"3. **Schema complexity matters** - more detailed schemas with more properties, descriptions, and constraints will consume more tokens\n",
"4. **Model differences** - Different models may have slightly different overhead for structured responses\n",
"\n",
"The token counting function above provides estimates that should be close to the actual API usage, but as with other token counting methods, consider these estimates rather than exact guarantees.\n"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- title: Automating Code Quality and Security Fixes with Codex CLI on GitLab
path: examples/codex/secure_quality_gitlab.md
date: 2025年08月29日
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.