← 返回首页 ❓ 面试题库
📑 本篇章节导航 (共7章,点击直达)
一、LLM网关概念与定位(1-6)二、智能路由策略(7-14)三、主流LLM网关工具(15-20)四、限流熔断与高可用(21-27)五、安全与合规(28-32)六、成本治理(33-37)七、实战与面试总结(38-50)

Q60 - LLM网关与模型路由实战

共50道题 | 难度分布:★★★★★平均 | 20K+ AI应用岗位LLM基础设施必考

一、LLM网关概念与定位(1-6)

LLM网关(AI Gateway)的定义?

★★★★★

为什么需要LLM网关?

★★★★★

LLM网关 vs API网关的区别?

★★★★

统一API接入设计?

★★★★


// 统一调用——后端可以是GPT-4o/Claude/通义千问
ChatClient chatClient = ChatClient.builder()
    .baseUrl("http://llm-gateway:8080/v1")  // 指向网关
    .apiKey("gateway-token")               // 网关统一Token
    .modelName("auto")                     // 网关决定路由
    .build();

多模型无缝切换?

★★★★

LLM网关的架构模式?

★★★★

二、智能路由策略(7-14)

基于模型的静态路由?

★★★★

基于权重的负载均衡路由?

★★★★


routes:
  - model: gpt-4o
    weight: 80
    endpoint: https://api.openai.com/v1
  - model: claude-sonnet
    weight: 20
    endpoint: https://api.anthropic.com/v1

基于请求复杂度的智能路由?

★★★★★


public String route(String prompt) {
    if (prompt.length() < 200 && isSimpleTask(prompt)) {
        return "gpt-4o-mini";  // 简单任务用便宜模型
    }
    return "gpt-4o";  // 复杂任务用强模型
}

基于Header特征的路由?

★★★★


if ($http_x_model_tier = "premium") {
    proxy_pass http://gpt-4o-backend;
}
if ($http_x_model_tier = "basic") {
    proxy_pass http://gpt-4o-mini-backend;
}

Fallback容错路由?

★★★★★


fallback_chain:
  - primary: gpt-4o
    timeout: 30s
    retry: 2
  - secondary: claude-sonnet
    timeout: 30s
    retry: 1
  - tertiary: qwen-max
    timeout: 30s

成本优化路由?

★★★★

地理位置路由?

★★★

路由策略的评估与调优?

★★★★

三、主流LLM网关工具(15-20)

One-API详解?

★★★★★

LiteLLM详解?

★★★★


from litellm import completion
response = completion(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
    fallbacks=["claude-sonnet", "qwen-max"]
)

Higress AI网关?

★★★★

Portkey AI Gateway?

★★★

工具选型对比总结?

★★★★

Spring Cloud Gateway作为LLM网关?

★★★★★


@Component
public class LlmRoutingFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange ex, GatewayFilterChain chain) {
        String prompt = ex.getRequest().getBody(); // 提取Prompt
        String model = routingStrategy.route(prompt); // 路由决策
        ex.getRequest().mutate().header("X-Target-Model", model);
        return chain.filter(ex);
    }
}

四、限流熔断与高可用(21-27)

LLM API限流策略?

★★★★★


@Bean
public RedisRateLimiter llmRateLimiter() {
    return new RedisRateLimiter(100, 200); // 100 replenishRate, 200 burstCapacity
}

熔断器设计?

★★★★


CircuitBreaker cb = CircuitBreaker.ofDefaults("gpt-4o")
    .toBuilder()
    .failureRateThreshold(50)
    .waitDurationInOpenState(Duration.ofSeconds(30))
    .build();

重试策略?

★★★★

语义缓存(Semantic Cache)?

★★★★★

多Key轮转管理?

★★★★

LLM网关的高可用架构?

★★★★

灰度发布与模型切换?

★★★★

五、安全与合规(28-32)

API密钥集中管理?

★★★★★

请求/响应过滤?

★★★★

审计日志?

★★★★

合规与数据出境?

★★★★

多租户隔离?

★★★★

六、成本治理(33-37)

Token消耗实时监控?

★★★★★

成本预算与告警?

★★★★

成本优化最佳实践?

★★★★

多模型成本对比Dashboard?

★★★★

按用户/租户成本分摊?

★★★★

七、实战与面试总结(38-50)

One-API部署实战?

★★★★


# docker-compose.yml
services:
  one-api:
    image: justsong/one-api
    ports:
      - "3000:3000"
    environment:
      - SQL_DSN=root:password@tcp(mysql:3306)/oneapi
      - REDIS_CONN_STRING=redis://redis:6379
    depends_on:
      - mysql
      - redis
  mysql:
    image: mysql:8
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: oneapi
  redis:
    image: redis:7

Spring Cloud Gateway + LLM路由完整实现?

★★★★★


@Configuration
public class LlmGatewayConfig {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
            .route("openai", r -> r
                .header("X-Target-Model", "gpt-4o")
                .uri("https://api.openai.com/v1"))
            .route("claude", r -> r
                .header("X-Target-Model", "claude")
                .uri("https://api.anthropic.com/v1"))
            .route("qwen", r -> r
                .header("X-Target-Model", "qwen")
                .uri("https://dashscope.aliyuncs.com/api/v1"))
            .build();
    }
}

@Component
public class TokenCountFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange ex, GatewayFilterChain chain) {
        return chain.filter(ex).doAfterTerminate(() -> {
            String usage = ex.getResponse().getHeaders().getFirst("X-Token-Usage");
            meterRegistry.counter("llm.tokens", "model", getModel(ex)).increment(parseTokens(usage));
        });
    }
}

LLM网关的监控Dashboard设计?

★★★★

LLM网关的性能优化?

★★★★

国产模型接入实践?

★★★★

LLM网关的测试策略?

★★★★

LLM网关的演进路线?

★★★

从零搭建LLM网关的架构设计?

★★★★★

LLM网关与Spring AI的集成?

★★★★


// Spring AI通过网关调用LLM
@Bean
public ChatModel chatModel() {
    return new OpenAiChatModel(
        OpenAiApi.builder()
            .baseUrl("http://llm-gateway:8080/v1")  // 网关地址
            .apiKey("gateway-token")                // 网关统一Token
            .build()
    );
}

// 网关层路由Filter根据请求自动选择模型
// 应用层无需关心底层用哪个模型
ChatResponse response = chatClient.prompt()
    .user("分析这段代码")
    .call()
    .chatResponse();

Server-Sent Events流式透传?

★★★★

LLM网关面试高频总结?

★★★★★

LLM网关的未来趋势?

★★★

LLM网关架构设计答题模板?

★★★★★