1# Prompt Forge - Code Patterns Reference
2
3Read this file when you need SDK-specific code examples for implementing prompts.
4
5## Claude SDK Patterns
6
7### System Prompt with Caching
8
9```python
10system=[{
11 "type": "text",
12 "text": SYSTEM_PROMPT,
13 "cache_control": {"type": "ephemeral"},
14}]
15```
16
17- Writing to cache costs 25% more than base input price
18- Reading from cache costs only 10% of base input price
19- Breakeven after ~4 calls with the same cached content
20
21### Structured Outputs (messages.parse)
22
23```python
24response = client.messages.parse(
25 model="claude-haiku-4-5",
26 max_tokens=1024,
27 system=[{"type": "text", "text": SYSTEM_PROMPT, "cache_control": {"type": "ephemeral"}}],
28 messages=[{"role": "user", "content": user_content}],
29 output_format=OutputSchema, # Pydantic model
30)
31result = response.parsed_output # typed instance
32```
33
34This is NOT tool use. It enforces the schema at the API level.
35
36### Prefilling
37
38```python
39messages=[
40 {"role": "user", "content": "Extract the data from this document: ..."},
41 {"role": "assistant", "content": "{"} # Forces JSON output
42]
43```
44
45Use sparingly - structured outputs are preferred for JSON.
46
47## OpenAI SDK Patterns
48
49### Structured Outputs
50
51```python
52response = client.chat.completions.create(
53 model="gpt-5.2",
54 messages=[...],
55 response_format={"type": "json_schema", "json_schema": {...}}
56)
57```
58
59### Reasoning Effort
60
61```python
62response = client.chat.completions.create(
63 model="gpt-5.2",
64 messages=[...],
65 reasoning_effort="medium" # none, minimal, low, medium, high, xhigh
66)
67```
68
69## Confidence Schema Pattern
70
71```python
72class ConfidenceLevel(str, Enum):
73 HIGH = "HIGH" # Clearly stated, unambiguous
74 MEDIUM = "MEDIUM" # Reasonable inference from context
75 LOW = "LOW" # Weak signal, likely needs human review
76 MISSING = "MISSING" # Not found in source
77
78class ExtractedData(BaseModel):
79 field_a: Optional[str] = None
80 field_a_confidence: ConfidenceLevel = ConfidenceLevel.MISSING
81 overall_confidence: ConfidenceLevel = ConfidenceLevel.MISSING
82 fields_needing_review: list[str] = []
83```
84