{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b0a26fc",
   "metadata": {},
   "source": [
    "# Token counting\n",
    "LangChain offers a context manager that allows you to count tokens."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "195fd686",
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "\n",
    "from langchain.callbacks import get_openai_callback\n",
    "from langchain.llms import OpenAI\n",
    "\n",
    "llm = OpenAI(temperature=0)\n",
    "with get_openai_callback() as cb:\n",
    "    llm(\"What is the square root of 4?\")\n",
    "\n",
    "total_tokens = cb.total_tokens\n",
    "assert total_tokens > 0\n",
    "\n",
    "with get_openai_callback() as cb:\n",
    "    llm(\"What is the square root of 4?\")\n",
    "    llm(\"What is the square root of 4?\")\n",
    "\n",
    "assert cb.total_tokens == total_tokens * 2\n",
    "\n",
    "# You can kick off concurrent runs from within the context manager\n",
    "with get_openai_callback() as cb:\n",
    "    await asyncio.gather(\n",
    "        *[llm.agenerate([\"What is the square root of 4?\"]) for _ in range(3)]\n",
    "    )\n",
    "\n",
    "assert cb.total_tokens == total_tokens * 3\n",
    "\n",
    "# The context manager is concurrency safe\n",
    "task = asyncio.create_task(llm.agenerate([\"What is the square root of 4?\"]))\n",
    "with get_openai_callback() as cb:\n",
    "    await llm.agenerate([\"What is the square root of 4?\"])\n",
    "\n",
    "await task\n",
    "assert cb.total_tokens == total_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e94e0d3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "venv"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
