File size: 7,053 Bytes
36cdf5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# [API Usage Examples with GPT-4.1](https://chatgpt.com/canvas/shared/6825f96694a48191af7648cad2996158)

## Overview

This guide provides detailed, real-world examples of using the OpenAI GPT-4.1 API effectively, with a focus on instruction-following, tool integration, agent persistence, and prompt structuring. These examples are designed to help developers and engineers build resilient, production-ready systems using GPT-4.1 across various applications, including customer service, bug fixing, document analysis, and data labeling.

Each example illustrates system prompt construction, tool schema definitions, interaction workflows, and failure mitigation strategies.



## Example 1: Customer Support Agent with Tool Use

### Objective

Deploy a GPT-4.1 assistant to handle user questions about policies and account status.

### System Prompt

```markdown
You are a helpful assistant for NewTelco. Your job is to assist users with account and policy information.

# Instructions
- Always greet the user.
- Use tools to retrieve account and policy information.
- If needed data is missing, ask the user before calling tools.
- Avoid internal knowledge on restricted topics.

# Workflow
1. Identify intent
2. Call tools if sufficient input
3. Message user before and after tool calls
4. Cite results
```

### Tools

```json
[
  {
    "name": "lookup_policy_document",
    "description": "Searches internal docs by topic",
    "parameters": {
      "type": "object",
      "properties": {
        "topic": {"type": "string"}
      },
      "required": ["topic"]
    }
  },
  {
    "name": "get_user_account_info",
    "description": "Retrieves account details using phone number",
    "parameters": {
      "type": "object",
      "properties": {
        "phone_number": {"type": "string"}
      },
      "required": ["phone_number"]
    }
  }
]
```

### User Input

```text
How much will it cost for international service in France?
```

### Assistant Output

```text
Hi, you've reached NewTelco, how can I help you? 🌍✈️

You're asking about international service while traveling to France. Let me check that for you β€” one moment, please. πŸ•‘
```

### Tool Call

```json
{
  "name": "lookup_policy_document",
  "arguments": {"topic": "international service cost France"}
}
```



## Example 2: Software Bug Fix Agent with Diff Application

### Objective

Use GPT-4.1 to investigate and fix a bug in a codebase using the `apply_patch` tool.

### System Prompt Highlights

```markdown
# Objective
Fix the issue provided by the user.

# Instructions
- Plan each step
- Reflect after each function call
- Never guess code β€” read it first using tools
- Only stop when all tests pass

# Workflow
1. Understand issue deeply
2. Investigate codebase
3. Draft patch
4. Apply patch
5. Run tests
6. Reflect and finalize
```

### Tool Definition

```json
{
  "name": "python",
  "description": "Execute code or apply a patch",
  "parameters": {
    "type": "object",
    "properties": {
      "input": {"type": "string"}
    },
    "required": ["input"]
  }
}
```

### Tool Call Example

```bash
%%bash
apply_patch <<"EOF"
*** Begin Patch
*** Update File: src/core.py
@@ def is_valid():
-    return False
+    return True
*** End Patch
EOF
```

### Test Execution

```json
{
  "name": "python",
  "arguments": {"input": "!python3 run_tests.py"}
}
```



## Example 3: Long-Context Document Analyzer

### Objective

Summarize and extract insights from up to 1M tokens of context.

### Prompt Sections

```markdown
# Instructions
- Process documents in 10k token blocks
- Reflect after each segment
- Label relevance and extract core ideas

# Strategy
1. Read β†’ summarize
2. Score relevance
3. Synthesize into unified output
```

### Input Format

```xml
<doc id="21" title="Policy Update">
<summary>Changes to international billing rules</summary>
<content>...</content>
</doc>
```

### Assistant Behavior

* Chunk input into 10k token sections
* After each, provide a summary and document scores
* Compile findings at end



## Example 4: Data Labeling Assistant

### Objective

Assist with structured classification tasks.

### Prompt Template

```markdown
# Instructions
- Label each entry using the provided schema
- Do not guess; if unsure, flag for human

# Labeling Categories
- Urgent
- Normal
- Spam

# Output Format
{"text": ..., "label": ...}

# Example
{"text": "Win money now!", "label": "Spam"}
```

### User Input

```json
[
  "New system update available",
  "Limited time offer! Click now",
  "Server crashed, need help ASAP"
]
```

### Assistant Output

```json
[
  {"text": "New system update available", "label": "Normal"},
  {"text": "Limited time offer! Click now", "label": "Spam"},
  {"text": "Server crashed, need help ASAP", "label": "Urgent"}
]
```



## Example 5: Chain-of-Thought for Multi-Hop Reasoning

### Objective

Support a planning task by explicitly breaking down the steps.

### Prompt Template

```markdown
# Instructions
First, think carefully step by step. Then output the result.

# Reasoning Strategy
1. Identify user question
2. Extract context
3. Connect information across documents
4. Output answer
```

### Example Input

```markdown
# User Question
How did the billing policy change after 2022?

# Context
<doc id="10" title="Policy 2022">...</doc>
<doc id="12" title="Policy 2023">...</doc>
```

### Model Output

```text
Step 1: Identify relevant documents β†’ IDs 10, 12
Step 2: Compare clauses
Step 3: 2022 had flat rates, 2023 added time-of-use billing
Answer: Billing policy changed to time-based pricing in 2023.
```



## General Prompt Formatting Guidelines

### Preferred Structure

```markdown
# Role
# Instructions
# Workflow (optional)
# Reasoning Strategy (optional)
# Output Format
# Examples (optional)
```

### Tool Use Reminders

* Only call tools when sufficient information is available
* Always notify the user before and after calls
* Use example-triggered calls for teaching tool behavior

### Output Patterns

* JSON or markdown preferred
* Cite source documents if used
* Include fallback responses if uncertain (e.g., "Insufficient context")



## Best Practices Summary

| Element      | Best Practice                                 |
| ------------ | --------------------------------------------- |
| Tool Calls   | Always define schema with strong param names  |
| Planning     | Enforce pre- and post-action reflection       |
| Output       | Enforce format, validate JSON before response |
| Long Context | Use structured delimiters (Markdown, XML)     |
| Labeling     | Use few-shot examples and explicit categories |
| Diff Format  | Use V4A patch format for code updates         |



## Final Note

These examples are starting templates. Each system will benefit from iterative refinements, structured logging, and real-world user testing. Maintain modular prompts and tool schemas, and adopt evaluation frameworks to monitor performance over time.

**Clarity, structure, and instruction adherence are the cornerstones of production-grade GPT-4.1 API design.**