{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Cron Jobs\n", "\n", "Sometimes you don't want to run your graph based on user interaction, but rather you would like to schedule your graph to run on a schedule - for example if you wish for your graph to compose and send out a weekly email of to-dos for your team. LangGraph Cloud allows you to do this without having to write your own script by using the `Crons` client. To schedule a graph job, you need to pass a [cron expression](https://crontab.cronhub.io/) to inform the client when you want to run the graph. `Cron` jobs are run in the background and do not interfere with normal invocations of the graph.\n", "\n", "## Setup\n", "\n", "First, let's setup our SDK client, assistant, and thread:" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [], "source": [ "from langgraph_sdk import get_client\n", "\n", "client = get_client()\n", "assistants = await client.assistants.search()\n", "assistants = [a for a in assistants if not a[\"config\"]]\n", "assistant = assistants[0]\n", "thread = await client.threads.create()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cron job on a thread \n", "\n", "To create a cron job associated with a specific thread, you can write:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This schedules a job to run at 15:27 (3:27PM) every day\n", "cron_1 = await client.crons.create_for_thread(\n", " thread[\"thread_id\"],\n", " assistant[\"assistant_id\"],\n", " schedule=\"27 15 * * *\",\n", " input={\"messages\": [{\"role\": \"user\", \"content\": \"What time is it?\"}]},\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that it is **very** important to delete `Cron` jobs that are no longer useful. Otherwise you could rack up unwanted API charges to the LLM! You can delete a `Cron` job using the following code:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "await client.crons.delete(cron_1[\"cron_id\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cron job stateless\n", "\n", "You can also create stateless cron jobs by using the following code:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This schedules a job to run at 15:27 (3:27PM) every day\n", "cron_2 = await client.crons.create(\n", " assistant[\"assistant_id\"],\n", " schedule=\"27 15 * * *\",\n", " input={\"messages\": [{\"role\": \"user\", \"content\": \"What time is it?\"}]},\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, remember to delete your job once you are done with it!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "await client.crons.delete(cron_2[\"cron_id\"])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.9" } }, "nbformat": 4, "nbformat_minor": 2 }