diff --git a/site/en/tutorials/python_quickstart.ipynb b/site/en/tutorials/python_quickstart.ipynb index a3be1f52f..167e74369 100644 --- a/site/en/tutorials/python_quickstart.ipynb +++ b/site/en/tutorials/python_quickstart.ipynb @@ -1034,6 +1034,64 @@ " display(to_markdown(f'**{message.role}**: {message.parts[0].text}'))" ] }, + { + "cell_type": "markdown", + "metadata": { + "id": "AEgVOYu0pAr4" + }, + "source": [ + "## Count tokens\n", + "\n", + "Large language models have a context window, and the context length is often measured in terms of the **number of tokens**. With the Gemini API, you can determine the number of tokens per any `glm.Content` object. In the simplest case, you can pass a query string to the `GenerativeModel.count_tokens` method as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "eLjBmPCLpElk" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total_tokens: 7" + ] + } + ], + "source": [ + "model.count_tokens(\"What is the meaning of life?\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oM2_U8pmpHQA" + }, + "source": [ + "Similarly, you can check `token_count` for your `ChatSession`:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "i0MUU4BZpG4_" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total_tokens: 501" + ] + } + ], + "source": [ + "model.count_tokens(chat.history)" + ] + }, { "cell_type": "markdown", "metadata": {