Generate Subgraph State Dynamically in Supervisor from User Request

Hello everyone! I’m using supervisor to understand which next graph to run (subgraph) from the user’s request. Each subgraph has its own state, and I want to make supervisor generate the state for the selected graph based on the user’s request (message).

Thanks in advance for your reply.

Hey Nikita!

If I understand correctly, your supervisor is going to triage a request, and then kick off a subgraph with Command(goto=“subgraph”, update={state for subgraph}} or an alternative.

There are a few design choices here:

  1. You could use withStructuredOutput alongside a custom prompt to generate the inputs for your graph, and then pass those in.

  2. You could treat each subgraph as a tool - then the LLM can choose which subgraph to call based on a prompt, and will generate the inputs to that tool. The tool can still .invoke() a graph object inside the function.

Option 1 sounds closer to what you have in mind.

Thank you very much for your response :slightly_smiling_face:.

Do I understand correctly that in the first approach, i need:

  1. Call the LLM first time to select the required graph from the user’s request
  2. Get its state (to pass to withStructuredOutput)
  3. Сall the LLM again to select the state from the request (for structured output)?
  4. Invoke subgraph with generated state

Yes that’s right

  1. LLM triages where to go - this should also probably be done using withStructuredOutput(). The LLM will return a string designating which sub agent to hand off to.
  2. Then once the destination has been chosen, you can get the input state (BaseModel, dict, etc.) back from a separate call using withStructuredOutput.
  3. Then you can manually invoke the subgraph, or navigate to it with Command (if the subgraph is a node in your main graph)
1 Like

Thanks! I’m running to implement it!