Skip to main content
The Functional API allows you to add LangGraph’s key features (persistence, memory, human-in-the-loop, and streaming) to your applications with minimal changes to your existing code.
For conceptual information on the functional API, see Functional API.

Creating a simple workflow

When defining an entrypoint, input is restricted to the first argument of the function. To pass multiple inputs, you can use a dictionary.
This example demonstrates how to use the @task and @entrypoint decorators syntactically. Given that a checkpointer is provided, the workflow results will be persisted in the checkpointer.

Parallel execution

Tasks can be executed in parallel by invoking them concurrently and waiting for the results. This is useful for improving performance in IO bound tasks (e.g., calling APIs for LLMs).
This example demonstrates how to run multiple LLM calls in parallel using @task. Each call generates a paragraph on a different topic, and results are joined into a single text output.
This example uses LangGraph’s concurrency model to improve execution time, especially when tasks involve I/O like LLM completions.

Calling graphs

The Functional API and the Graph API can be used together in the same application as they share the same underlying runtime.

Call other entrypoints

You can call other entrypoints from within an entrypoint or a task.

Streaming

The Functional API uses the same streaming mechanism as the Graph API. Please read the streaming guide section for more details. Example of using the streaming API to stream value chunks from a workflow run.
  1. Emit custom data before computation begins.
  2. Emit another custom message after computing the result.
  3. Use streamEvents() to process streamed output.
  4. Iterate stream.values to receive value snapshots.

Retry policy

Caching tasks

  1. ttl is specified in seconds. The cache will be invalidated after this time.

Resuming after an error

When we resume execution, we won’t need to re-run the slowTask as its result is already saved in the checkpoint.

Human-in-the-loop

The functional API supports human-in-the-loop workflows using the interrupt function and the Command primitive.

Basic human-in-the-loop workflow

We will create three tasks:
  1. Append "bar".
  2. Pause for human input. When resuming, append human input.
  3. Append "qux".
We can now compose these tasks in an entrypoint:
interrupt() is called inside a task, enabling a human to review and edit the output of the previous task. The results of prior tasks— in this case step_1— are persisted, so that they are not run again following the interrupt. Let’s send in a query string:
Note that we’ve paused with an interrupt after step_1. The interrupt provides instructions to resume the run. To resume, we issue a Command containing the data expected by the human_feedback task.
After resuming, the run proceeds through the remaining step and terminates as expected.

Review tool calls

To review tool calls before execution, we add a review_tool_call function that calls interrupt. When this function is called, execution will be paused until we issue a command to resume it. Given a tool call, our function will interrupt for human review. At that point we can either:
  • Accept the tool call
  • Revise the tool call and continue
  • Generate a custom tool message (e.g., instructing the model to re-format its tool call)
We can now update our entrypoint to review the generated tool calls. If a tool call is accepted or revised, we execute in the same way as before. Otherwise, we just append the ToolMessage supplied by the human. The results of prior tasks—in this case the initial model call—are persisted, so that they are not run again following the interrupt.

Short-term memory

Short-term memory allows storing information across different invocations of the same thread id. See short-term memory for more details.

Manage checkpoints

You can view and delete the information stored by the checkpointer.

View thread state

View the history of the thread

Decouple return value from saved value

Use entrypoint.final to decouple what is returned to the caller from what is persisted in the checkpoint. This is useful when:
  • You want to return a computed result (e.g., a summary or status), but save a different internal value for use on the next invocation.
  • You need to control what gets passed to the previous parameter on the next run.

Chatbot example

An example of a simple chatbot using the functional API and the InMemorySaver checkpointer. The bot is able to remember the previous conversation and continue from where it left off.

Long-term memory

long-term memory allows storing information across different thread ids. This could be useful for learning information about a given user in one conversation and using it in another.

Workflows

  • Workflows and agent guide for more examples of how to build workflows using the Functional API.

Integrate with other libraries