Before diving into the runnable interface and pass-through components, this lesson reviews three primary ways to invoke a chain in LangChain:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- Synchronous invocation (
invoke) - Streaming output (
stream) - Batch processing (
batch)
1. Synchronous Invocation (invoke)
Execute the entire chain in one call and receive the complete response:
chain.invoke(...) will block until the LLM has generated the full response. Use this for simpler or short-prompt scenarios.2. Streaming Output (stream)
When you need a “typing” effect or to process tokens on the fly, use streaming:
Streaming large models can generate many tokens. Monitor your usage and consider rate limits.
3. Batch Processing (batch)
Process multiple inputs in parallel to improve throughput:
Invocation Methods Comparison
| Method | Description | Example |
|---|---|---|
| invoke | Synchronous, returns full result | chain.invoke({...}) |
| stream | Yields tokens/chunks as they’re generated | for chunk in chain.stream(...) |
| batch | Runs multiple inputs concurrently | chain.batch([{…}, {…}]) |
What’s Next?
In the following sections, we’ll explore:- The Runnable Interface: How to wrap custom logic into reusable components
- Pass-Through Components: Techniques for chaining without transforming the data