Skip to main content
This lesson demonstrates three common ways to execute LangChain-style chains so you can choose the right execution mode for your application: synchronous invocation (invoke), token-level streaming (stream), and parallel/batch execution (batch). Each example composes a prompt template, a chat LLM, and a simple string output parser into a single chain.

Synchronous execution (invoke)

Synchronous invocation runs the entire chain and returns the final output only after the LLM call completes. Use this for straightforward requests where you need the full response before proceeding. It’s a blocking call that waits for the model to finish generating.
When to use invoke:
  • Simpler workflows where latency is acceptable.
  • When you must process the final, fully formed output atomically.
  • Integrations that cannot easily handle partial/streamed responses.

Streaming execution (stream)

Streaming yields text chunks progressively as the LLM generates tokens. This is ideal for live typing effects, improving perceived responsiveness, or updating a UI in real time. Note that streaming requires an LLM client that supports it (some clients need streaming=True).
Best practices for streaming:
  • Handle partial tokens and join chunks carefully if you need complete sentences.
  • Provide visual indicators in your UI for streaming states (e.g., loading spinner, “typing…” animation).
  • Fall back to invoke when streaming is not supported by the LLM client.

Parallel / batch execution (batch)

Batch mode executes the same chain for multiple inputs, potentially in parallel (implementation-dependent). It returns a list of results matching the order of inputs.
When to choose batch:
  • High-throughput scenarios where you need to process many prompts.
  • Workloads that can tolerate increased concurrency and API calls.
  • Bulk processing where responses do not depend on each other.
Streaming improves perceived responsiveness and enables typing-style UIs. Confirm your LLM client supports streaming and remember that chunks may contain partial words or tokens — plan to reassemble or display incremental content safely.
Batch/parallel execution increases concurrency and API usage. Monitor rate limits, quotas, and cost when using batch mode, and apply throttling or retries as needed.

Quick comparison

Summary

  • Use invoke for straightforward synchronous calls that return the final output.
  • Use stream to receive incremental chunks during generation to enable live or typing UIs.
  • Use batch to process multiple inputs at once and maximize throughput.

Watch Video