Skip to content

6.2 Connect to a deployment ​

This chapter reorganizes the upstream notebook and Python implementation into a web lesson. The teaching notes were prepared for this site with a matching Chinese edition.

Source notebook · Python implementation and configuration

Version and environment

Code follows the upstream snapshot dated 2025-10-12, not a compatibility promise for current SDKs. Original code cells appear below in notebook order, including Jupyter magic such as %pip / %%capture; execute them separately in a notebook. Configure credentials locally, never in a web page or Git. This site renders lessons; the paid model calls and Docker exercises have not been executed here.

Runs, threads and stores ​

Use langgraph_sdk.get_client with the server URL. A run is one execution, a thread groups conversation checkpoints, and the store holds cross-thread memories. RemoteGraph is another interface when composing with LangGraph itself.

Background and streaming runs ​

runs.create starts a background execution; runs.get reads its status and runs.join waits for completion. Joining one run does not globally serialize other clients. The snapshot streams message events with messages-tuple; event names and payload shapes must match your installed server and SDK versions.

Inspect, fork and share memory ​

Read thread state and history before editing a checkpoint. Reusing a message ID replaces that message through the reducer rather than appending. The deployed graph’s ToDo namespace is (todo, todo_category, user_id), unlike the simpler Module 5 example; the default category is general.

Original code cells ​

Comments and identifiers remain as supplied upstream. Cells share variables from earlier cells; saved outputs and historical screenshots are not results from this website.

Cell 1 ​

python
%%capture --no-stderr
%pip install -U langgraph_sdk

Cell 2 ​

python
from langgraph_sdk import get_client

# Connect via SDK
url_for_cli_deployment = "http://localhost:8123"
client = get_client(url=url_for_cli_deployment)

Cell 3 ​

python
%%capture --no-stderr
%pip install -U langchain_openai langgraph langchain_core

Cell 4 ​

python
from langgraph.pregel.remote import RemoteGraph
from langchain_core.messages import convert_to_messages
from langchain_core.messages import HumanMessage, SystemMessage

# Connect via remote graph
url = "http://localhost:8123"
graph_name = "task_maistro"
remote_graph = RemoteGraph(graph_name, url=url)

Cell 5 ​

python
# Create a thread
thread = await client.threads.create()
thread

Cell 6 ​

python
# Check any existing runs on a thread
thread = await client.threads.create()
runs = await client.runs.list(thread["thread_id"])
print(runs)

Cell 7 ​

python
# Ensure we've created some ToDos and saved them to my user_id
user_input = "Add a ToDo to finish booking travel to Hong Kong by end of next week. Also, add a ToDo to call parents back about Thanksgiving plans."
config = {"configurable": {"user_id": "Test"}}
graph_name = "task_maistro"
run = await client.runs.create(thread["thread_id"], graph_name, input={"messages": [HumanMessage(content=user_input)]}, config=config)

Cell 8 ​

python
# Kick off a new thread and a new run
thread = await client.threads.create()
user_input = "Give me a summary of all ToDos."
config = {"configurable": {"user_id": "Test"}}
graph_name = "task_maistro"
run = await client.runs.create(thread["thread_id"], graph_name, input={"messages": [HumanMessage(content=user_input)]}, config=config)

Cell 9 ​

python
# Check the run status
print(await client.runs.get(thread["thread_id"], run["run_id"]))

Cell 10 ​

python
# Wait until the run completes
await client.runs.join(thread["thread_id"], run["run_id"])
print(await client.runs.get(thread["thread_id"], run["run_id"]))

Cell 11 ​

python
user_input = "What ToDo should I focus on first."
async for chunk in client.runs.stream(thread["thread_id"],
                                      graph_name,
                                      input={"messages": [HumanMessage(content=user_input)]},
                                      config=config,
                                      stream_mode="messages-tuple"):

    if chunk.event == "messages":
        print("".join(data_item['content'] for data_item in chunk.data if 'content' in data_item), end="", flush=True)

Cell 12 ​

python
thread_state = await client.threads.get_state(thread['thread_id'])
for m in convert_to_messages(thread_state['values']['messages']):
    m.pretty_print()

Cell 13 ​

python
# Copy the thread
copied_thread = await client.threads.copy(thread['thread_id'])

Cell 14 ​

python
# Check the state of the copied thread
copied_thread_state = await client.threads.get_state(copied_thread['thread_id'])
for m in convert_to_messages(copied_thread_state['values']['messages']):
    m.pretty_print()

Cell 15 ​

python
# Get the history of the thread
states = await client.threads.get_history(thread['thread_id'])

# Pick a state update to fork
to_fork = states[-2]
to_fork['values']

Cell 16 ​

python
to_fork['values']['messages'][0]['id']

Cell 17 ​

python
to_fork['next']

Cell 18 ​

python
to_fork['checkpoint_id']

Cell 19 ​

python
forked_input = {"messages": HumanMessage(content="Give me a summary of all ToDos that need to be done in the next week.",
                                         id=to_fork['values']['messages'][0]['id'])}

# Update the state, creating a new checkpoint in the thread
forked_config = await client.threads.update_state(
    thread["thread_id"],
    forked_input,
    checkpoint_id=to_fork['checkpoint_id']
)

Cell 20 ​

python
# Run the graph from the new checkpoint in the thread
async for chunk in client.runs.stream(thread["thread_id"],
                                      graph_name,
                                      input=None,
                                      config=config,
                                      checkpoint_id=forked_config['checkpoint_id'],
                                      stream_mode="messages-tuple"):

    if chunk.event == "messages":
        print("".join(data_item['content'] for data_item in chunk.data if 'content' in data_item), end="", flush=True)

Cell 21 ​

python
items = await client.store.search_items(
    ("todo", "general", "Test"),
    limit=5,
    offset=0
)
items['items']

Cell 22 ​

python
from uuid import uuid4
await client.store.put_item(
    ("testing", "Test"),
    key=str(uuid4()),
    value={"todo": "Test SDK put_item"},
)

Cell 23 ​

python
items = await client.store.search_items(
    ("testing", "Test"),
    limit=5,
    offset=0
)
items['items']

Cell 24 ​

python
[item['key'] for item in items['items']]

Cell 25 ​

python
await client.store.delete_item(
       ("testing", "Test"),
        key='3de441ba-8c79-4beb-8f52-00e4dcba68d4',
    )

Cell 26 ​

python
items = await client.store.search_items(
    ("testing", "Test"),
    limit=5,
    offset=0
)
items['items']

Practice and checks ​

Create a run, wait for it, inspect thread state, and copy the thread. Search the same user’s ToDos in the general namespace from another thread. Use only test keys for the notebook’s delete example.

For the full original narrative, saved outputs and diagrams, consult the notebook.

English translation of community learning materials. MIT licensed.