Introduction to Inputs and Outputs#
When working with model APIs, understanding how inputs and outputs are handled is crucial for generating accurate and relevant text. Inputs are the prompts or data you provide to the model, while outputs are the responses generated by the model. Effective management of these elements ensures that your application can handle various types of data seamlessly.
- Inputs can be simple text prompts or structured data.
- Outputs typically include text responses, but can also contain structured data like JSON.
- It's important to validate and sanitize inputs to prevent injection attacks and ensure data integrity.
const response = await client.responses.create({ model: "gpt-6.2-astra", input: "Write a one-sentence bedtime story about a unicorn." });Handling Outputs#
The output from a model API is usually an array of content items. Each item can contain various types of data, including text, tool calls, and reasoning tokens. Properly handling these outputs ensures that your application can interpret and utilize the data effectively.
- Do not assume the text output is always at
output[0].content[0].text. - Some SDKs provide a convenient
output_textproperty that aggregates all text outputs. - It's beneficial to check the structure of the output to handle different types of data appropriately.
console.log(response.output_text);Prompt Engineering Techniques#
Effective prompt engineering ensures that the model generates content that meets your requirements. This involves writing clear and specific instructions and using message roles to guide the model's behavior. Properly crafted prompts can significantly enhance the quality and relevance of the generated text.
- Use the
instructionsparameter to give high-level instructions. - Utilize message roles like
developeranduserto provide context and authority. - Consider using structured data in your prompts to provide more detailed information to the model.
const response = await client.responses.create({ model: "gpt-6.2-astra", instructions: "Talk like a pirate.", input: "Are semicolons optional in JavaScript?" });Token Counting and Management#
Token counting helps you manage the number of tokens in your prompts, which is essential for staying within rate limits and optimizing costs. Different models and message components consume different numbers of tokens. Understanding token usage allows you to fine-tune your prompts for better performance and efficiency.
- Use the token counting endpoint to estimate the number of tokens in your messages.
- Avoid server tools and MCP connectors when counting tokens, as they are not supported.
- Regularly monitor your token usage to identify areas for optimization.
response = client.messages.count_tokens(model="claude-opus-5-5", messages=[{"role": "user", "content": "Hello, Claude"}]);Practical Example: Generating a Bedtime Story#
Let's walk through an example of generating a one-sentence bedtime story using the OpenAI API. We'll use TypeScript to demonstrate the process. This example will cover importing the necessary libraries, creating a client instance, making a request to the API, and logging the output text to the console.
- Import the OpenAI library and create a client instance.
- Call the
responses.createmethod with a model and input prompt. - Log the output text to the console.
- Handle potential errors gracefully to ensure robust application behavior.
import OpenAI from "openai";
const client = new OpenAI();
try {
const response = await client.responses.create({ model: "gpt-6.2-astra", input: "Write a one-sentence bedtime story about a unicorn." });
console.log(response.output_text);
} catch (error) {
console.error("Error generating story:", error);
}Verification Checklist#
To ensure your implementation is correct, verify the following points:
- The model name is correctly specified.
- The input prompt is well-formed and clear.
- The output text is logged correctly.
- Token counting is used to estimate the number of tokens in the prompt.
- Error handling is implemented to manage potential issues gracefully.
Sources & methodology
These references were selected by the editorial team. Verify examples against current documentation and your project before using them.
- Text generation | OpenAI APIAdded: 2026-11-22
- Token counting - Claude Platform DocsAdded: 2026-11-22
Turn the idea into reviewable work.
Bring your models, editor, terminal and browser together in Devura.
Get Devura


