How to kick off background runs¶
This guide covers how to kick off background runs for your agent. This can be useful for long running jobs.
In [1]:
Copied!
# Initialize the client
from langgraph_sdk import get_client
client = get_client()
# Initialize the client
from langgraph_sdk import get_client
client = get_client()
In [2]:
Copied!
# List available assistants
assistants = await client.assistants.search()
assistants[0]
# List available assistants
assistants = await client.assistants.search()
assistants[0]
Out[2]:
{'assistant_id': 'e90fee30-be91-43aa-a33c-d54bd219072e', 'graph_id': 'agent', 'created_at': '2024-06-18T18:06:55.102231+00:00', 'updated_at': '2024-06-18T18:06:55.102231+00:00', 'config': {'configurable': {'model_name': 'anthropic'}}, 'metadata': {}}
In [3]:
Copied!
# NOTE: we can use `assistant_id` UUID from the above response, or just pass graph ID instead when creating runs. we'll use graph ID here
assistant_id = "agent"
# NOTE: we can use `assistant_id` UUID from the above response, or just pass graph ID instead when creating runs. we'll use graph ID here
assistant_id = "agent"
In [4]:
Copied!
# Create a new thread
thread = await client.threads.create()
thread
# Create a new thread
thread = await client.threads.create()
thread
Out[4]:
{'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d', 'created_at': '2024-06-21T14:58:02.079462+00:00', 'updated_at': '2024-06-21T14:58:02.079462+00:00', 'metadata': {}}
In [5]:
Copied!
# If we list runs on this thread, we can see it is empty
runs = await client.runs.list(thread["thread_id"])
runs
# If we list runs on this thread, we can see it is empty
runs = await client.runs.list(thread["thread_id"])
runs
Out[5]:
[]
In [6]:
Copied!
# Let's kick off a run
input = {"messages": [{"role": "human", "content": "what's the weather in sf"}]}
run = await client.runs.create(thread["thread_id"], assistant_id, input=input)
# Let's kick off a run
input = {"messages": [{"role": "human", "content": "what's the weather in sf"}]}
run = await client.runs.create(thread["thread_id"], assistant_id, input=input)
In [7]:
Copied!
# The first time we poll it, we can see `status=pending`
await client.runs.get(thread["thread_id"], run["run_id"])
# The first time we poll it, we can see `status=pending`
await client.runs.get(thread["thread_id"], run["run_id"])
Out[7]:
{'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198', 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d', 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca', 'created_at': '2024-06-21T14:58:02.095911+00:00', 'updated_at': '2024-06-21T14:58:02.095911+00:00', 'metadata': {}, 'status': 'pending', 'kwargs': {'input': {'messages': [{'role': 'human', 'content': 'what's the weather in sf'}]}, 'config': {'metadata': {'created_by': 'system'}, 'configurable': {'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198', 'user_id': '', 'graph_id': 'agent', 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d', 'thread_ts': None, 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'}}, 'webhook': None, 'temporary': False, 'stream_mode': ['events'], 'feedback_keys': None, 'interrupt_after': None, 'interrupt_before': None}, 'multitask_strategy': 'reject'}
In [8]:
Copied!
# Wait until the run finishes
await client.runs.join(thread["thread_id"], run["run_id"])
# Wait until the run finishes
await client.runs.join(thread["thread_id"], run["run_id"])
In [9]:
Copied!
# Eventually, it should finish and we should see `status=success`
await client.runs.get(thread["thread_id"], run["run_id"])
# Eventually, it should finish and we should see `status=success`
await client.runs.get(thread["thread_id"], run["run_id"])
Out[9]:
{'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198', 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d', 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca', 'created_at': '2024-06-21T14:58:02.095911+00:00', 'updated_at': '2024-06-21T14:58:02.095911+00:00', 'metadata': {}, 'status': 'success', 'kwargs': {'input': {'messages': [{'role': 'human', 'content': 'what's the weather in sf'}]}, 'config': {'metadata': {'created_by': 'system'}, 'configurable': {'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198', 'user_id': '', 'graph_id': 'agent', 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d', 'thread_ts': None, 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'}}, 'webhook': None, 'temporary': False, 'stream_mode': ['events'], 'feedback_keys': None, 'interrupt_after': None, 'interrupt_before': None}, 'multitask_strategy': 'reject'}
In [10]:
Copied!
# We can get the final results
final_result = await client.threads.get_state(thread["thread_id"])
# We can get the final results
final_result = await client.threads.get_state(thread["thread_id"])
In [11]:
Copied!
final_result
final_result
Out[11]:
{'values': {'messages': [{'content': 'what's the weather in sf', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': 'bfe07fff-cb40-40be-84d5-a061d2c40006', 'example': False}, {'content': [{'id': 'toolu_01QUzhhfDQkpbPSediUrXvQb', 'input': {'query': 'weather in san francisco'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-6d8665ca-a77d-4b44-9a7b-4e975b155fb1', 'example': False, 'tool_calls': [{'name': 'tavily_search_results_json', 'args': {'query': 'weather in san francisco'}, 'id': 'toolu_01QUzhhfDQkpbPSediUrXvQb'}], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': '[{"url": "https://www.timeanddate.com/weather/usa/san-francisco/historic", "content": "San Francisco Weather History for the Previous 24 Hours Show weather for: Previous 24 hours June 17, 2024 June 16, 2024 June 15, 2024 June 14, 2024 June 13, 2024 June 12, 2024 June 11, 2024 June 10, 2024 June 9, 2024 June 8, 2024 June 7, 2024 June 6, 2024 June 5, 2024 June 4, 2024 June 3, 2024 June 2, 2024"}]', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'tool', 'name': 'tavily_search_results_json', 'id': '257a1f29-2f66-4f9e-b35d-c8818dbbaa3f', 'tool_call_id': 'toolu_01QUzhhfDQkpbPSediUrXvQb'}, {'content': [{'text': 'The search results provide historic weather data for San Francisco, but do not give the current weather conditions. To get the current weather forecast for San Francisco, I would need to refine my search query. Here is an updated search:', 'type': 'text'}, {'id': 'toolu_01RLJEcWYRvRoBhiHdrhoRZx', 'input': {'query': 'san francisco weather forecast today'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-ca41dbf8-7e89-4ff2-a245-87098d7928ba', 'example': False, 'tool_calls': [{'name': 'tavily_search_results_json', 'args': {'query': 'san francisco weather forecast today'}, 'id': 'toolu_01RLJEcWYRvRoBhiHdrhoRZx'}], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': '[{"url": "https://www.weatherapi.com/", "content": "{\'location\': {\'name\': \'San Francisco\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 37.78, \'lon\': -122.42, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1718981382, \'localtime\': \'2024-06-21 7:49\'}, \'current\': {\'last_updated_epoch\': 1718981100, \'last_updated\': \'2024-06-21 07:45\', \'temp_c\': 12.8, \'temp_f\': 55.0, \'is_day\': 1, \'condition\': {\'text\': \'Overcast\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/122.png\', \'code\': 1009}, \'wind_mph\': 6.9, \'wind_kph\': 11.2, \'wind_degree\': 200, \'wind_dir\': \'SSW\', \'pressure_mb\': 1011.0, \'pressure_in\': 29.84, \'precip_mm\': 0.01, \'precip_in\': 0.0, \'humidity\': 86, \'cloud\': 100, \'feelslike_c\': 12.2, \'feelslike_f\': 53.9, \'windchill_c\': 11.2, \'windchill_f\': 52.1, \'heatindex_c\': 12.0, \'heatindex_f\': 53.5, \'dewpoint_c\': 9.4, \'dewpoint_f\': 48.8, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 3.0, \'gust_mph\': 7.6, \'gust_kph\': 12.2}}"}]', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'tool', 'name': 'tavily_search_results_json', 'id': 'c80a3720-6a9f-4ff0-9ce2-6112e66a6f81', 'tool_call_id': 'toolu_01RLJEcWYRvRoBhiHdrhoRZx'}, {'content': 'The updated search provides the current weather forecast for San Francisco. According to the results, as of 7:49am on June 21, 2024 in San Francisco, the temperature is 55°F (12.8°C), it is overcast with 100% cloud cover, and there are light winds from the south-southwest around 7 mph (11 km/h). The forecast also shows low precipitation of 0.01 mm, high humidity of 86%, and visibility of 9 miles (16 km).\n\nIn summary, the current weather in San Francisco is cool, overcast, and breezy based on this weather forecast data. Let me know if you need any other details!', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-4f23b53d-a8ec-4038-b3ed-08b2560bf81c', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]}, 'next': [], 'config': {'configurable': {'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d', 'thread_ts': '1ef2fdea-f879-65a5-8005-443b6a4039aa'}}, 'metadata': {'step': 5, 'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198', 'source': 'loop', 'writes': {'agent': {'messages': [{'id': 'run-4f23b53d-a8ec-4038-b3ed-08b2560bf81c', 'name': None, 'type': 'ai', 'content': 'The updated search provides the current weather forecast for San Francisco. According to the results, as of 7:49am on June 21, 2024 in San Francisco, the temperature is 55°F (12.8°C), it is overcast with 100% cloud cover, and there are light winds from the south-southwest around 7 mph (11 km/h). The forecast also shows low precipitation of 0.01 mm, high humidity of 86%, and visibility of 9 miles (16 km).\n\nIn summary, the current weather in San Francisco is cool, overcast, and breezy based on this weather forecast data. Let me know if you need any other details!', 'example': False, 'tool_calls': [], 'usage_metadata': None, 'additional_kwargs': {}, 'response_metadata': {}, 'invalid_tool_calls': []}]}}, 'user_id': '', 'graph_id': 'agent', 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d', 'created_by': 'system', 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'}, 'created_at': '2024-06-21T14:58:14.591805+00:00', 'parent_config': {'configurable': {'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d', 'thread_ts': '1ef2fdea-d44c-6fc4-8004-d2713436777d'}}}
In [12]:
Copied!
# We can get the content of the final message
final_result["values"]["messages"][-1]["content"]
# We can get the content of the final message
final_result["values"]["messages"][-1]["content"]
Out[12]:
'The updated search provides the current weather forecast for San Francisco. According to the results, as of 7:49am on June 21, 2024 in San Francisco, the temperature is 55°F (12.8°C), it is overcast with 100% cloud cover, and there are light winds from the south-southwest around 7 mph (11 km/h). The forecast also shows low precipitation of 0.01 mm, high humidity of 86%, and visibility of 9 miles (16 km).\n\nIn summary, the current weather in San Francisco is cool, overcast, and breezy based on this weather forecast data. Let me know if you need any other details!'