El procesamiento Flex reduce los costos de las solicitudes a Responses o Chat Completions a cambio de tiempos de respuesta más largos y falta ocasional de recursos. Es ideal para tareas fuera de producción o de menor prioridad, como evaluaciones de modelos, enriquecimiento de datos y cargas de trabajo asíncronas.
Los tokens se cobran según las tarifas de la API de procesamiento por lotes, con descuentos adicionales por el almacenamiento de prompts en caché.
El procesamiento Flex está en versión beta y solo está disponible para algunos modelos. Los modelos compatibles
se indican en la página de precios.
Para usar el procesamiento Flex, establece el parámetro service_tier en flex en tu solicitud a la API:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import OpenAI from "openai";
const client = new OpenAI({
timeout: 15 * 1000 * 60, // Increase default timeout to 15 minutes
});
const response = await client.responses.create(
{
model: "gpt-6-astra",
instructions: "List and describe all the metaphors used in this book.",
input: "<very long text of book here>",
service_tier: "flex",
},
{ timeout: 15 * 1000 * 60 }
);
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16from openai import OpenAI
client = OpenAI(
# increase default timeout to 15 minutes (from 10 minutes)
timeout=900.0
)
# you can override the max timeout per request as well
response = client.with_options(timeout=900.0).responses.create(
model="gpt-6-astra",
instructions="List and describe all the metaphors used in this book.",
input="<very long text of book here>",
service_tier="flex",
)
print(response.output_text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package main
import (
"context"
"fmt"
"time"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient(option.WithRequestTimeout(15 * time.Minute))
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Instructions: openai.String("List and describe all the metaphors used in this book."),
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("<very long text of book here>")},
ServiceTier: responses.ResponseNewParamsServiceTierFlex,
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import java.time.Duration;
client = client.withOptions(options -> options.timeout(Duration.ofMinutes(15)));
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input("<very long text of book here>")
.instructions("List and describe all the metaphors used in this book.")
.serviceTier(ResponseCreateParams.ServiceTier.FLEX)
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19using System.ClientModel;
using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClientOptions clientOptions = new() { NetworkTimeout = TimeSpan.FromMinutes(15) };
ResponsesClient client = new(new ApiKeyCredential(key), clientOptions);
CreateResponseOptions options = new()
{
Model = "gpt-6-astra",
Instructions = "List and describe all the metaphors used in this book.",
ServiceTier = ResponseServiceTier.Flex,
};
options.InputItems.Add(ResponseItem.CreateUserMessageItem("<very long text of book here>"));
using CancellationTokenSource timeout = new(TimeSpan.FromMinutes(15));
ResponseResult response = await client.CreateResponseAsync(options, timeout.Token);
Console.WriteLine(response.GetOutputText());
1
2
3
4
5
6
7
8
9
10
11
12require "openai"
client = OpenAI::Client.new(timeout: 900.0)
response = client.responses.create(
model: "gpt-6-astra",
service_tier: :flex,
instructions: "List and describe all the metaphors used in this book.",
input: "<very long text of book here>"
)
puts(response.output_text)
1
2
3
4
5
6
7
8
9curl https://api.openai.com/v1/responses \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6-astra",
"instructions": "List and describe all the metaphors used in this book.",
"input": "<very long text of book here>",
"service_tier": "flex"
}'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21import OpenAI from "openai";
const client = new OpenAI({
timeout: 15 * 1000 * 60,
});
const response = await client.chat.completions.create(
{
model: "gpt-6-astra",
messages: [
{
role: "developer",
content: "List and describe all the metaphors used in this book.",
},
{ role: "user", content: "<very long text of book here>" },
],
service_tier: "flex",
},
{ timeout: 15 * 1000 * 60 }
);
console.log(response.choices[0].message.content);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18from openai import OpenAI
client = OpenAI(timeout=900.0)
response = client.chat.completions.create(
model="gpt-6-astra",
messages=[
{
"role": "developer",
"content": "List and describe all the metaphors used in this book.",
},
{"role": "user", "content": "<very long text of book here>"},
],
service_tier="flex",
timeout=900.0,
)
print(response.choices[0].message.content)
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
26package main
import (
"context"
"fmt"
"time"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
)
func main() {
client := openai.NewClient(option.WithRequestTimeout(15 * time.Minute))
completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "gpt-6-astra",
ServiceTier: openai.ChatCompletionNewParamsServiceTierFlex,
Messages: []openai.ChatCompletionMessageParamUnion{
openai.DeveloperMessage("List and describe all the metaphors used in this book."),
openai.UserMessage("<very long text of book here>"),
},
})
if err != nil {
panic(err)
}
fmt.Println(completion.Choices[0].Message.Content)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import java.time.Duration;
client = client.withOptions(options -> options.timeout(Duration.ofMinutes(15)));
ChatCompletionCreateParams params =
ChatCompletionCreateParams.builder()
.model("gpt-6-astra")
.addDeveloperMessage("List and describe all the metaphors used in this book.")
.addUserMessage("<very long text of book here>")
.serviceTier(ChatCompletionCreateParams.ServiceTier.FLEX)
.build();
client.chat().completions().create(params).choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22using System.ClientModel;
using OpenAI;
using OpenAI.Chat;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-6-astra";
OpenAIClientOptions clientOptions = new() { NetworkTimeout = TimeSpan.FromMinutes(15) };
ChatClient client = new(model, new ApiKeyCredential(key), clientOptions);
ChatCompletionOptions options = new() { ServiceTier = ChatServiceTier.Flex };
using CancellationTokenSource timeout = new(TimeSpan.FromMinutes(15));
ChatCompletion completion = await client.CompleteChatAsync(
[
new DeveloperChatMessage("List and describe all the metaphors used in this book."),
new UserChatMessage("<very long text of book here>"),
],
options,
timeout.Token
);
Console.WriteLine(completion.Content[0].Text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20require "openai"
client = OpenAI::Client.new(timeout: 900.0)
completion = client.chat.completions.create(
model: "gpt-6-astra",
service_tier: :flex,
messages: [
{
role: :developer,
content: "List and describe all the metaphors used in this book."
},
{
role: :user,
content: "<very long text of book here>"
}
]
)
puts(completion.choices.fetch(0).message.content)
1
2
3
4
5
6
7
8curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{
"model": "gpt-6-astra",
"messages": [
{"role": "developer", "content": "List and describe all the metaphors used in this book."},
{"role": "user", "content": "<very long text of book here>"}
],
"service_tier": "flex"
}' --max-time 900
Debido a la menor velocidad del procesamiento Flex, es más probable que se agote el tiempo de espera de las solicitudes. Ten en cuenta lo siguiente para manejar estos casos:
- Tiempo de espera predeterminado: el tiempo de espera predeterminado es de 10 minutos al realizar solicitudes a la API con un SDK oficial de OpenAI. Es posible que debas aumentarlo para prompts extensos o tareas complejas.
- Configuración de los tiempos de espera: cada SDK ofrece un parámetro para aumentar este tiempo de espera. En los SDK de Python y JavaScript, este parámetro es
timeout, como se muestra en los ejemplos de código anteriores.
- Reintentos automáticos: los SDK de OpenAI reintentan automáticamente dos veces las solicitudes que devuelven el código de error
408 Request Timeout antes de lanzar una excepción.
En ocasiones, el procesamiento Flex puede no contar con suficientes recursos para atender tus solicitudes, lo que genera el código de error 429 Resource Unavailable. No se te cobrará cuando esto ocurra.
Considera implementar estas estrategias para manejar los errores por falta de recursos disponibles:
-
Reintenta las solicitudes con espera exponencial: implementar una espera exponencial es adecuado para cargas de trabajo que toleran demoras y busca minimizar los costos, ya que tu solicitud puede completarse cuando haya más capacidad disponible. Para obtener detalles de implementación, consulta este cookbook.
-
Reintenta las solicitudes con procesamiento estándar: cuando recibas un error por falta de recursos disponibles, implementa una estrategia de reintentos con procesamiento estándar si, para tu caso de uso, vale la pena asumir costos ocasionalmente más altos para garantizar que la solicitud se complete correctamente. Para hacerlo, establece service_tier en auto en la solicitud que reintentes, o elimina el parámetro service_tier para usar el modo predeterminado del proyecto.