> For the complete documentation index, see [llms.txt](https://jig.gitbook.io/sley/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jig.gitbook.io/sley/examples/python.md).

# Python examples

Browse complete Python projects by learning goal, Sley mechanism, and cognitive complexity.

Do not choose by ambition. Choose the smallest project that answers your next question, run it, and change one behavior. Complexity estimates cognitive load, not quality; lower is usually the more useful starting point.

## Start here

| Project                                                                                                                                                                                                                                | What it teaches                                                       | Sley mechanisms                 | Complexity |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- | ------------------------------- | ---------: |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-hello-world/README.md">Sley Hello World</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-hello-world/README.md">Read the lesson</a></p>     | Create a node, use shared state, and finish an ordinary leaf.         | `node`, `state`, `run`          |        2.5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-node/README.md">Retry and Recovery</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-node/README.md">Read the lesson</a></p>                 | Retry a complete handler and recover locally after attempts fail.     | `retry`, `recover`, `Failure`   |          3 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-flow/README.md">Text Converter Flow</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-flow/README.md">Read the lesson</a></p>                | Connect a readable two-node cycle and deliberately hard-end one path. | `link`, `Flow`, `end`           |          4 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-communication/README.md">Shared Run State</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-communication/README.md">Read the lesson</a></p> | Share durable facts between handlers through run state.               | `state`, `link`, `run`          |          4 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-workflow/README.md">Article Writing Workflow</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-workflow/README.md">Read the lesson</a></p>   | Model a three-step application workflow as explicit topology.         | `node`, `link`, `Flow`          |        6.5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-async-basic/README.md">Async Recipe Finder</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-async-basic/README.md">Read the lesson</a></p>  | Await ordinary service I/O inside an async handler.                   | `async handler`, `state`, `run` |        4.5 |

## Branch, batch, and combine

| Project                                                                                                                                                                                                                                                                | What it teaches                                                         | Sley mechanisms                        | Complexity |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------- | -------------------------------------- | ---------: |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-batch/README.md">Sequential Batch Translation</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-batch/README.md">Read the lesson</a></p>                                     | Dispatch repeated work while the Flow admits one branch at a time.      | `emit`, `input`, `end`                 |          4 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-batch-node/README.md">Combine CSV Chunks</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-batch-node/README.md">Read the lesson</a></p>                                     | Publish chunk statistics and combine them after every branch settles.   | `emit`, `end`, `combine`               |        4.5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-parallel-batch/README.md">Parallel Batch Translation</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-parallel-batch/README.md">Read the lesson</a></p>                     | Run independent translation branches with local concurrency.            | `emit`, `input`, `concurrency`         |          5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-batch-flow/README.md">Reusing a Flow for Batch Work</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-batch-flow/README.md">Read the lesson</a></p>                          | Reuse a nested Flow as one branch worker.                               | `nested Flow`, `emit`, `end`           |          6 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-parallel-batch-flow/README.md">Sequential and Parallel Nested Flows</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-parallel-batch-flow/README.md">Read the lesson</a></p> | Compare sequential and concurrent nested scopes.                        | `nested Flow`, `concurrency`, `end`    |          7 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-majority-vote/README.md">Majority Vote</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-majority-vote/README.md">Read the lesson</a></p>                                    | Collect several independent answers and choose a majority once.         | `emit`, `end`, `combine`               |          6 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-best-of-n-judge/README.md">Blind Best-of-N Judge</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-best-of-n-judge/README.md">Read the lesson</a></p>                        | Generate distinct candidates and select through blind pairwise judging. | `emit`, `end`, `combine`               |          9 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-map-reduce/README.md">Resume Qualification with Map/Reduce</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-map-reduce/README.md">Read the lesson</a></p>                   | Keep Map and Reduce visible as application nodes.                       | `emit`, `end`, `combine`               |          8 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-nested-batch/README.md">Nested Batch Flow Example</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-nested-batch/README.md">Read the lesson</a></p>                          | Coordinate two levels of scoped fan-out and joining.                    | `nested Flow`, `end`, `combine`        |        9.5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-resilient-batch/README.md">Recovering a Partially Completed Batch</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-resilient-batch/README.md">Read the lesson</a></p>       | Recover a failed batch while preserving settled branch results.         | `ScopeFailure`, `terminals`, `recover` |          6 |

## Agents and reasoning

| Project                                                                                                                                                                                                                                                         | What it teaches                                                            | Sley mechanisms                             | Complexity |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------------------------------------------- | ---------: |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-agent/README.md">Research Agent</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-agent/README.md">Read the lesson</a></p>                                            | Build a bounded search-and-answer loop from named decisions.               | `emit`, `named link`, `max_activations`     |          7 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-thinking/README.md">Iterative Plan Refinement</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-thinking/README.md">Read the lesson</a></p>                           | Refine a compact plan through validated, bounded iterations.               | `named link`, `max_activations`, `retry`    |          5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-orchestrator-workers/README.md">Orchestrator–Workers–Editor</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-orchestrator-workers/README.md">Read the lesson</a></p> | Freeze a dynamic plan, fan sections out, and integrate the results.        | `emit`, `input`, `combine`                  |         10 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-multi-agent/README.md">Multi-Agent Taboo Game</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-multi-agent/README.md">Read the lesson</a></p>                        | Let independent Flows communicate through application queues.              | `Flow`, `state`, `run`                      |          7 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-mcp/README.md">Model Context Protocol Tools</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-mcp/README.md">Read the lesson</a></p>                                  | Discover, validate, and invoke one MCP tool in a linear Flow.              | `async handler`, `input`, `validation`      |          8 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-supervisor/README.md">Evaluator–Optimizer Supervisor</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-supervisor/README.md">Read the lesson</a></p>                  | Revise against structured independent feedback until approval or a stop.   | `exits`, `nested Flow`, `max_activations`   |          9 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-quality-loop/README.md">Reference-Grounded Quality Loop</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-quality-loop/README.md">Read the lesson</a></p>             | Compose benchmarked component loops, integration, and blind final judging. | `nested Flow`, `combine`, `max_activations` |         15 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-a2a/README.md">External Task Protocol Adapter</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-a2a/README.md">Read the lesson</a></p>                                | Bridge a Flow to an application-owned JSON-RPC task boundary.              | `run`, `state`, `max_activations`           |         12 |

## Structured data and retrieval

| Project                                                                                                                                                                                                                                         | What it teaches                                                        | Sley mechanisms                          | Complexity |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | ---------------------------------------- | ---------: |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-structured-output/README.md">Structured Output</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-structured-output/README.md">Read the lesson</a></p> | Parse and validate external structured output before committing state. | `state`, `retry`, `validation`           |          3 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-tool-database/README.md">SQLite Tool</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-tool-database/README.md">Read the lesson</a></p>               | Keep a linear database workflow visible and testable.                  | `link`, `state`, `Flow`                  |          5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-text2sql/README.md">Text to SQL</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-text2sql/README.md">Read the lesson</a></p>                         | Inspect a schema, execute SQL, and route failures into a repair loop.  | `emit`, `named link`, `max_activations`  |          8 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-chat-memory/README.md">Chat with Memory Retrieval</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-chat-memory/README.md">Read the lesson</a></p>    | Combine recent state with retrieved history in a chat loop.            | `state`, `named link`, `max_activations` |          7 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-rag/README.md">Retrieval-Augmented Generation</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-rag/README.md">Read the lesson</a></p>                | Separate offline indexing from an online retrieval Flow.               | `nested Flow`, `emit`, `combine`         |          9 |

## Tools and media

| Project                                                                                                                                                                                                                                               | What it teaches                                                       | Sley mechanisms                         | Complexity |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- | --------------------------------------- | ---------: |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-tool-embeddings/README.md">OpenAI Embeddings</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-tool-embeddings/README.md">Read the lesson</a></p>           | Keep a provider call outside graph definition and inject it normally. | `node`, `state`, `run`                  |          3 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-tool-search/README.md">Web Search with Analysis</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-tool-search/README.md">Read the lesson</a></p>            | Search and analyze results through a small linear Flow.               | `link`, `state`, `Flow`                 |          5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-tool-pdf-vision/README.md">PDF Vision Batch</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-tool-pdf-vision/README.md">Read the lesson</a></p>            | Fan out PDF files and combine their page-level vision results.        | `emit`, `end`, `combine`                |          6 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-tool-crawler/README.md">Web Crawler with Content Analysis</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-tool-crawler/README.md">Read the lesson</a></p> | Crawl once, fan pages out, and combine one report.                    | `emit`, `end`, `combine`                |        9.5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-llm-streaming/README.md">Stream an LLM Response</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-llm-streaming/README.md">Read the lesson</a></p>          | Stream provider output while application code owns interruption.      | `node`, `state`, `run`                  |          4 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-voice-chat/README.md">Voice Chat</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-voice-chat/README.md">Read the lesson</a></p>                            | Connect speech input, response, and playback in a visible loop.       | `emit`, `named link`, `max_activations` |          7 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-visualization/README.md">Flow Visualization</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-visualization/README.md">Read the lesson</a></p>              | Render portable static topology from a compiled Flow.                 | `compile`, `describe`, `Flow`           |          7 |

## Interactive applications

| Project                                                                                                                                                                                                                                        | What it teaches                                                      | Sley mechanisms                             | Complexity |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------- | ------------------------------------------- | ---------: |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-chat/README.md">Simple Chat</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-chat/README.md">Read the lesson</a></p>                                | Repeat a one-node conversation until an ordinary leaf exit.          | `named link`, `state`, `max_activations`    |          3 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-chat-guardrail/README.md">Travel Chat Guardrail</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-chat-guardrail/README.md">Read the lesson</a></p>  | Validate user input before a model call and route rejection clearly. | `emit`, `named link`, `state`               |          5 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-streamlit-hitl/README.md">Streamlit Human Review</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-streamlit-hitl/README.md">Read the lesson</a></p> | Let a UI own staged human review around short Flow runs.             | `run`, `state`, `Flow`                      |          4 |
| <p><a href="https://github.com/jigmd/sley/tree/main/cookbook/python-fastapi-hitl/README.md">Web Human Review</a><br><a href="https://github.com/jigmd/sley/blob/main/cookbook/python-fastapi-hitl/README.md">Read the lesson</a></p>           | Keep one short run alive while web requests deliver human feedback.  | `async handler`, `state`, `max_activations` |          8 |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://jig.gitbook.io/sley/examples/python.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
