A Prompt Template that i use to Save Tokens and Improve Results

API documentation, such as that from Stripe, Twilio, or OpenAI, can be voluminous: pages filled with examples, verbose explanations, and peripheral sections. When feeding an AI directly with all this content, you face:
Excessive token consumption: Models like GPT-4 or Grok count tokens for input and output, and long texts can quickly exhaust quotas.
Hallucinations: The AI may misinterpret or invent details, especially if the prompt isn’t focused.
Vague responses: Without structure, the AI tends to generate conversational outputs rather than direct code or clear instructions.
The solution? Pre-process the documentation with an optimizing prompt, creating a condensed version that serves as the basis for future queries.
The process is straightforward and can be applied on any AI platform that supports long prompts:
Documentation Collection: Gather all relevant API documentation. This may include web pages, PDFs, or text files. Copy the raw content (without extra formatting).
Initial AI Input: Send the complete documentation to the AI in an initial prompt. For example: "Here is the full documentation for the [API Name] API: [paste text here]. Now, use the following template to optimize it."
Template Application: Provide the prompt template (detailed below) immediately after. The AI will process the documentation and generate a structured output in JSON or Markdown.
Subsequent Use: Save the optimized output and use it as context in future prompts. For instance, instead of resending the full doc (thousands of tokens), you send only the structured version (hundreds of tokens) and ask specific questions, like "Generate Python code for endpoint X using this optimized doc."
This workflow transforms a 10,000-token documentation into a 1,000-2,000-token version, retaining all essential information. On platforms like base44, where "vibe coding" involves rapid iterations, this speeds up development and reduces costs.
Here is the exact template I use. It instructs the AI to act as an expert in technical documentation, focusing on extracting and structuring key elements for consumption by LLMs. The goal is to create a parseable output, free of fluff, ready for tasks like code generation or integration.
Act as an expert in code documentation and technical writing. Your task is to transform the provided content from a website or document into a structured, highly optimized format specifically for Large Language Models (LLMs) used in code generation, integration assistance, and API consumption.
Focus on:
1. **Code Examples:** Extract and clearly label all code snippets, distinguishing between different languages (e.g., JavaScript, Python, cURL, etc.). Ensure they are runnable or clearly demonstrate syntax.
2. **API Endpoints:** Identify and structure API endpoint details (HTTP methods, URLs, request/response bodies, authentication).
3. **Integration Steps:** Break down complex integration processes into clear, sequential steps.
4. **Key Concepts/Glossary:** Define any domain-specific terms or concepts essential for understanding.
5. **Troubleshooting/Error Handling:** Include common issues and their resolutions.
6. **Prerequisites/Dependencies:** List any necessary setups or libraries.
Organize the output into a JSON or Markdown format that is easily parseable and directly consumable by LLMs for tasks like:
- Generating code based on documentation.
- Answering specific questions about API usage.
- Providing integration instructions.
- Creating SDKs or libraries.
Maintain accuracy and conciseness. Remove any conversational filler or marketing language. Prioritize actionable information.
Example Structure (adapt as necessary):
```json
{
"title": "API Documentation for [Project/Service Name]",
"overview": "Concise summary of the project/service and its primary purpose.",
"authentication": {
"method": "API Key/OAuth/Token",
"details": "How to obtain and use authentication credentials."
},
"endpoints": [
{
"name": "Get User Data",
"path": "/users/{id}",
"method": "GET",
"description": "Retrieves details for a specific user.",
"parameters": [
{"name": "id", "type": "string", "required": true, "description": "User ID"}
],
"request_example": "curl -X GET 'https://api.example.com/users/123'",
"response_example": { "status": 200, "body": "{'id': '123', 'name': 'John Doe'}" }
}
],
"code_examples": [
{
"language": "Python",
"description": "Example of listing all users.",
"code": "import requests\nresponse = requests.get('https://api.example.com/users')\nprint(response.json())"
},
{
"language": "JavaScript",
"description": "Example of creating a new item.",
"code": "fetch('/api/items', { method: 'POST', body: JSON.stringify({ name: 'New Item' }) });"
}
],
"integration_guides": [
{
"title": "Integrating with Your Frontend",
"steps": [
"1. Install client library: `npm install @example/sdk`",
"2. Initialize: `const sdk = new ExampleSDK('YOUR_API_KEY');`",
"3. Make requests: `sdk.getUsers().then(data => console.log(data));`"
]
}
],
"troubleshooting": [
{
"issue": "401 Unauthorized",
"solution": "Ensure your API key is correct and included in the 'Authorization' header."
}
],
"glossary": [
{"term": "API Key", "definition": "A unique identifier used to authenticate a user or application."},
{"term": "Webhook", "definition": "An HTTP callback that notifies you of events."}
]
}If content is not suitable for a specific section (e.g., no code examples), omit that section. Output only the structured content.
The template’s flexibility allows omission of irrelevant sections, with JSON ensuring compatibility for scripts or other AIs.
## Practical Advantages
- **Token Reduction**: Tests with real docs (e.g., GitHub API) show a 70-80% size reduction, enabling more iterations within limits.
- **Minimized Hallucinations**: The structured format keeps the AI grounded in extracted facts.
- **Enhanced Productivity**: Ideal for rapid prototyping on [New Site Name], this speeds up integrations, such as generating custom SDKs.
- **Broad Applicability**: Compatible with any LLM, from open-source models like Llama to proprietary ones like Claude or Grok.
## Real-World Example
Consider the Weatherstack API documentation. Processed with this template, it might yield a JSON with the `/current` endpoint (GET for weather data), Python examples, and troubleshooting for invalid keys. You could then request: "Generate code to integrate this into a Flask app" using only the JSON.
## Conclusion
This template is a vital resource for developers using AI for technical tasks. It transforms cluttered documentation into a clear, efficient format, saving resources and improving outcomes. Try it on your next API project and adjust as needed. Do you use a similar approach, or have suggestions to enhance it? Share your thoughts below!Do you use a similar template for optimizing API documentation, or have tips to improve this approach?
0
8
1