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.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 needstreaming=True).
- 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
invokewhen 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.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
invokefor straightforward synchronous calls that return the final output. - Use
streamto receive incremental chunks during generation to enable live or typing UIs. - Use
batchto process multiple inputs at once and maximize throughput.
Links and References
- LangChain Documentation
- OpenAI API — Responses & Streaming
- LangChain — Chat Models & Streaming Patterns