> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flexinference.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 快速入门

> 从零开始，发送您的第一个请求，以更低的成本层级运行并及时返回结果。

使用您现有的提供商密钥，通过 FlexInference 发送一个请求。将您的 OpenAI 兼容客户端指向 FlexInference，添加 `start_within` 参数，FlexInference 将首先尝试更经济的 flex 层级，然后再升级到 standard 层级。它适用于 OpenAI、Gemini 和 Anthropic，您还可以通过固定[云路由](/zh/models)来通过 Amazon Bedrock 或 Google Vertex AI 提供相同的模型。对于 flex 请求，FlexInference 会收取节省金额的 20%，如果没有节省则不收取任何费用。

## 先决条件

* 一个有可用额度的 [OpenAI API 密钥](https://platform.openai.com/api-keys)。FlexInference 将您的密钥作为 BYOK 使用，OpenAI 会直接向您收费。
* 一个带有 `curl` 的终端，或 Python 3.9+，或 Node.js 18+。

## 开始使用

<Steps>
  <Step title="创建 FlexInference 账户">
    前往[控制面板](https://www.flexinference.com/dashboard)并登录。您的账户是一个独立的组织。它拥有您的 API 密钥并跟踪使用情况，使您的密钥和账单与其他组织分开。
  </Step>

  <Step title="创建 API 密钥">
    在控制面板中，创建一个密钥。它以 `flex_live_...` 开头，并且只显示一次。请立即保存它。将此密钥发送给 FlexInference，而不是您的 OpenAI 密钥。
  </Step>

  <Step title="添加您的 OpenAI 密钥 (BYOK)">
    将您的 OpenAI 密钥粘贴到控制面板中。FlexInference 会加密存储它，并且仅用于代表您发出的请求；您无需在每个请求中发送它。详情请参阅[身份验证](/zh/authentication)。
  </Step>

  <Step title="发送您的第一个请求">
    将您的客户端指向 `https://api.flexinference.com/v1` 并添加 `start_within` 参数。这里的 `00h-00m-30s` 表示请求最多可以等待 30 秒才开始。FlexInference 会首先尝试 OpenAI 的 flex 层级，因为它成本较低但可能需要排队。如果 flex 在 30 秒内无法启动，FlexInference 将转而运行正常的 standard 层级。Standard 是全价层级，会立即启动。

    Flex 是更经济的层级，它在启动前可能需要排队。Standard 是正常的全价层级，它会立即启动。FlexInference 是价格向上而非质量向下：它从 flex 开始，仅当 flex 无法满足您的时间限制时才使用 standard。

    `start_within` 是请求开始前的最大等待时间。将其写成持续时间格式，例如 `00h-00m-30s` 表示 30 秒。更大的值会给 flex 更多时间启动，从而可能节省更多费用。更小的值会更快地将请求转移到 standard。Claude 示例传递 `default` 而不是持续时间，因为 Anthropic 没有 flex 层级可供竞争，因此请求会直接进入 standard。该值限制的是启动时间，而不是总生成时间。有关值的完整列表，请参阅路由页面。

    <CodeGroup>
      ```bash curl theme={null}
      curl https://api.flexinference.com/v1/responses \
        -H "Authorization: Bearer $FLEX_API_KEY" \
        -H "Content-Type: application/json" \
        -d '{
          "model": "gpt-5-nano",
          "input": "Write a haiku about cheap GPUs.",
          "start_within": "00h-00m-30s"
        }'
      ```

      ```python Python (openai SDK) theme={null}
      from openai import OpenAI

      client = OpenAI(
          base_url="https://api.flexinference.com/v1",
          api_key="flex_live_...",  # your FlexInference key
      )

      resp = client.responses.create(
          model="gpt-5-nano",
          input="Write a haiku about cheap GPUs.",
          extra_body={"start_within": "00h-00m-30s"},
      )
      print(resp.output_text)
      ```

      ```typescript Node (openai SDK) theme={null}
      import OpenAI from "openai";

      const client = new OpenAI({
        baseURL: "https://api.flexinference.com/v1",
        apiKey: "flex_live_...", // your FlexInference key
      });

      const resp = await client.responses.create({
        model: "gpt-5-nano",
        input: "Write a haiku about cheap GPUs.",
        // start_within is a FlexInference extension; cast to pass it through.
        start_within: "00h-00m-30s",
      } as any);

      console.log(resp.output_text);
      ```

      ```bash curl (Gemini, Interactions) theme={null}
      curl https://api.flexinference.com/v1/interactions \
        -H "Authorization: Bearer $FLEX_API_KEY" \
        -H "Content-Type: application/json" \
        -d '{
          "model": "gemini-3.5-flash",
          "input": "Write a haiku about cheap GPUs.",
          "start_within": "00h-00m-30s"
        }'
      ```

      ```bash curl (Claude, Messages) theme={null}
      curl https://api.flexinference.com/v1/messages \
        -H "Authorization: Bearer $FLEX_API_KEY" \
        -H "Content-Type: application/json" \
        -d '{
          "model": "claude-opus-4-8",
          "max_tokens": 1024,
          "messages": [{ "role": "user", "content": "Write a haiku about cheap GPUs." }],
          "start_within": "default"
        }'
      ```
    </CodeGroup>
  </Step>
</Steps>

<Check>
  如果响应为 `200` 且 `"service_tier": "flex"`，则表示 flex 及时启动并在此调用中节省了费用。
  `"service_tier": "default"` 表示 FlexInference 使用了 standard，因为 flex 会超出您的时间限制。
  响应头 `x-flexinference-flex-applied` 和 `x-flexinference-flex-reason` 为代码公开了相同的结果。
  无论哪种方式，请求都会按时完成。
</Check>

<Note>
  如果您的第一个请求失败，请阅读错误正文。FlexInference 错误会说明哪里出了问题、原因、如何修复，并包含一个工作示例。如果您尚未在控制面板中添加您的 OpenAI 密钥，错误会指出这一点并指向修复该问题的页面。提供商错误会以其原始状态和正文传递。有关完整列表，请参阅错误页面。
</Note>

## 接下来尝试什么

<CardGroup cols={2}>
  <Card title="设置您的时间限制" icon="hourglass-half" href="/zh/deadline-routing">
    了解 `start_within` 的值以及将请求从 flex 转移到 standard 的规则。
  </Card>

  <Card title="流式传输、工具、视觉" icon="code" href="/zh/sdks">
    在 OpenAI、Gemini 和 Anthropic 中使用流式传输、工具和视觉功能。
  </Card>
</CardGroup>
