Dynamic Graph Creation at Runtime

As I continue to use Langgraph I am noticing that most of my use cases fall into the realm of dynamically needing to construct a graph (at runtime). I think most of the time I am finding myself gravitating towards this pattern as I can support more use cases using one graph definition/template and then dynamically reading my input to create more nodes if needed. Rather than building a graph for each granular use case, you can define graphs to be templates (nodes are more just high level concepts that know how to handle different inputs).

With that being said, I am not sure if this is a pattern people are trending towards but I am really curious about how langgraph users are setting themselves up to dynamically handle more complex tasks that still fall under their use case but with many potential variations. I would love to see how users are properly wiring up the graph dynamically. I have a way of doing this and I am seeing success but I feel like I am on an island as I don’t see a lot of examples with this.

I think the crux of this is that for now I want to avoid massive data dumps into the context window for my agents and the way to do that is to break it up into smaller pieces (break up the task into a graph of nodes where each node can be a particular step to get the overall task done). This workflow construction can be most effective if I react to the input and set up the graph with flexibility to handle the input. I would hate to create a graph for some use case and have to label it Workflow A with Input A, Workflow B with Input B….. My goal as a software engineer is to map use cases to some high level graph/workflow definition and then see if I can support many use cases so that I can have one base graph and have it be flexible enough to handle the use case. Obviously I am not trying to create ONE graph for ALL but obviously it can become hard to maintain a lot of graph definitions especially the ones that map to a similar use case with just slight variations.

The best example I can think of is with a planning or orchestration agent that will delegate tasks. Those tasks don’t have to map directly to already configured nodes…. maybe nodes can be templates but if your agent comes back with 10+ tasks, you want to be able to construct 10+ nodes to handle that task. Same goes from a graph that is dependent on some data structure I pass to it. Let’s say my graph relies on a nested json and that nested json changes based on the user’s request. My graph sets up the right nodes because naturally I want to break it up into pieces and on demand I construct what the graph will look like given the input data.

Are there any patterns for this that I can use to reference? Again I have this working pretty well but more looking for validation or code examples that show this off.

2 Likes

Hello,

Have you already looked at how langgraph supervisor is set up? I’m not too familiar with it, but langgraph swarm should do something similar as well.

Does that match up with you interpretation of dynamic graphs?

I also find the idea of Assistants in LangGraph Platform quite handy, as it allows you to instantiate a deployed graph with a different configuration.

I would be interested to know how you have set up the dynamic graphs. Or if you have any thoughts on the langgraph-supervisor approach and the idea of configurable assistants.

Hi there, you can consider using Send after dynamically generating nodes.