|
| 1 | +# Agent Patterns |
| 2 | + |
| 3 | +Common agentic patterns extended with Temporal's durable execution capabilities. |
| 4 | + |
| 5 | +*Adapted from [OpenAI Agents SDK agent patterns](https://github.com/openai/openai-agents-python/tree/main/examples/agent_patterns)* |
| 6 | + |
| 7 | +Before running these examples, be sure to review the [prerequisites and background on the integration](../README.md). |
| 8 | + |
| 9 | +## Running the Examples |
| 10 | + |
| 11 | +First, start the worker (supports all patterns): |
| 12 | +```bash |
| 13 | +uv run openai_agents/agent_patterns/run_worker.py |
| 14 | +``` |
| 15 | + |
| 16 | +Then run individual examples in separate terminals: |
| 17 | + |
| 18 | +### Deterministic Flows |
| 19 | +Sequential agent execution with validation gates - demonstrates breaking complex tasks into smaller steps: |
| 20 | +```bash |
| 21 | +uv run openai_agents/agent_patterns/run_deterministic_workflow.py |
| 22 | +``` |
| 23 | + |
| 24 | +### Parallelization |
| 25 | +Run multiple agents in parallel and select the best result - useful for improving quality or reducing latency: |
| 26 | +```bash |
| 27 | +uv run openai_agents/agent_patterns/run_parallelization_workflow.py |
| 28 | +``` |
| 29 | + |
| 30 | +### LLM-as-a-Judge |
| 31 | +Iterative improvement using feedback loops - generate content, evaluate it, and improve until satisfied: |
| 32 | +```bash |
| 33 | +uv run openai_agents/agent_patterns/run_llm_as_a_judge_workflow.py |
| 34 | +``` |
| 35 | + |
| 36 | +### Agents as Tools |
| 37 | +Use agents as callable tools within other agents - enables composition and specialized task delegation: |
| 38 | +```bash |
| 39 | +uv run openai_agents/agent_patterns/run_agents_as_tools_workflow.py |
| 40 | +``` |
| 41 | + |
| 42 | +### Agent Routing and Handoffs |
| 43 | +Route requests to specialized agents based on content analysis (adapted for non-streaming): |
| 44 | +```bash |
| 45 | +uv run openai_agents/agent_patterns/run_routing_workflow.py |
| 46 | +``` |
| 47 | + |
| 48 | +### Input Guardrails |
| 49 | +Pre-execution validation to prevent unwanted requests - demonstrates safety mechanisms: |
| 50 | +```bash |
| 51 | +uv run openai_agents/agent_patterns/run_input_guardrails_workflow.py |
| 52 | +``` |
| 53 | + |
| 54 | +### Output Guardrails |
| 55 | +Post-execution validation to detect sensitive content - ensures safe responses: |
| 56 | +```bash |
| 57 | +uv run openai_agents/agent_patterns/run_output_guardrails_workflow.py |
| 58 | +``` |
| 59 | + |
| 60 | +### Forcing Tool Use |
| 61 | +Control tool execution strategies - choose between different approaches to tool usage: |
| 62 | +```bash |
| 63 | +uv run openai_agents/agent_patterns/run_forcing_tool_use_workflow.py |
| 64 | +``` |
| 65 | + |
| 66 | +## Pattern Details |
| 67 | + |
| 68 | +### Deterministic Flows |
| 69 | +A common tactic is to break down a task into a series of smaller steps. Each task can be performed by an agent, and the output of one agent is used as input to the next. For example, if your task was to generate a story, you could break it down into the following steps: |
| 70 | + |
| 71 | +1. Generate an outline |
| 72 | +2. Check outline quality and genre |
| 73 | +3. Write the story (only if outline passes validation) |
| 74 | + |
| 75 | +Each of these steps can be performed by an agent. The output of one agent is used as input to the next. |
| 76 | + |
| 77 | +### Parallelization |
| 78 | +Running multiple agents in parallel is a common pattern. This can be useful for both latency (e.g. if you have multiple steps that don't depend on each other) and also for other reasons e.g. generating multiple responses and picking the best one. |
| 79 | + |
| 80 | +### LLM-as-a-Judge |
| 81 | +LLMs can often improve the quality of their output if given feedback. A common pattern is to generate a response using a model, and then use a second model to provide feedback. You can even use a small model for the initial generation and a larger model for the feedback, to optimize cost. |
| 82 | + |
| 83 | +### Agents as Tools |
| 84 | +The mental model for handoffs is that the new agent "takes over". It sees the previous conversation history, and owns the conversation from that point onwards. However, this is not the only way to use agents. You can also use agents as a tool - the tool agent goes off and runs on its own, and then returns the result to the original agent. |
| 85 | + |
| 86 | +### Guardrails |
| 87 | +Related to parallelization, you often want to run input guardrails to make sure the inputs to your agents are valid. For example, if you have a customer support agent, you might want to make sure that the user isn't trying to ask for help with a math problem. |
| 88 | + |
| 89 | +You can definitely do this without any special Agents SDK features by using parallelization, but we support a special guardrail primitive. Guardrails can have a "tripwire" - if the tripwire is triggered, the agent execution will immediately stop and a `GuardrailTripwireTriggered` exception will be raised. |
| 90 | + |
| 91 | +This is really useful for latency: for example, you might have a very fast model that runs the guardrail and a slow model that runs the actual agent. You wouldn't want to wait for the slow model to finish, so guardrails let you quickly reject invalid inputs. |
| 92 | + |
| 93 | +## Omitted Examples |
| 94 | + |
| 95 | +The following patterns from the [reference repository](https://github.com/openai/openai-agents-python/tree/main/examples/agent_patterns) are not included in this Temporal adaptation: |
| 96 | + |
| 97 | +- **Streaming Guardrails**: Requires streaming capabilities which are not yet available in the Temporal integration |
0 commit comments