# LLM Configuration Setup

## Environment Variables

Add the following to your `.env` or application configuration:

```bash
# Primary LLM: OpenAI (Modal API - less costly)
export OPENAI_API_KEY=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
export OPENAI_MODEL=gpt-3.5-turbo

# Secondary LLM: Claude (Anthropic)
export ANTHROPIC_API_KEY=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
export CLAUDE_MODEL=claude-3-sonnet-20240229

# Fallback: Llama (local or API)
export LLAMA_MODEL=llama-2-7b
# export LLAMA_LOCAL_PATH=/path/to/llama/model  # Only if using local model
```

## Configuration in Code

```elixir
# Usage in Phase 1 AI-Agentic Proposal Generation
config = MwKernel.LlmConfig.config()
# Primary: OpenAI (least costly)
# Secondary: Claude (if OpenAI fails)
# Fallback: Llama (if both fail)

# Make LLM calls with automatic fallback
{:ok, {provider, response}} = MwKernel.LlmConfig.call_llm(
  "Generate a flow for this use case...",
  model: "gpt-3.5-turbo",
  max_tokens: 2048
)
```

## Security Best Practices

1. **Never commit API keys** - Use environment variables only
2. **Use .env.local** - For development, ignored by git
3. **Rotate keys regularly** - Especially if exposed
4. **Principle of least privilege** - Use read-only tokens where possible
5. **Monitor usage** - Check OpenAI/Anthropic dashboards for unusual activity

## Cost Optimization Strategy

1. **Primary (OpenAI)** - Cheapest option for most tasks
   - gpt-3.5-turbo: ~$0.0005 per 1K input tokens
   - Ideal for flow generation, validation

2. **Secondary (Claude)** - Better for complex reasoning
   - Claude 3 Sonnet: ~$0.003 per 1K input tokens
   - Used as fallback for edge cases

3. **Fallback (Llama)** - Self-hosted or API
   - Free if self-hosted
   - Better privacy for sensitive operations

## Docker/Production Setup

For production, use secret management:

```dockerfile
# In docker-compose.yml or K8s secrets
OPENAI_API_KEY=<secret>
ANTHROPIC_API_KEY=<secret>
```

For AWS/Azure:
- Use Systems Manager Parameter Store (AWS)
- Use Azure Key Vault
- Load at container startup, never in code

## Testing

Mock the LLM config in tests:

```elixir
# test/support/llm_mock.ex
defmodule LlmMock do
  def call_llm(prompt, _opts) do
    {:ok, {:openai, "Mock response for: #{prompt}"}}
  end
end

# In test: stub with mock
Application.put_env(:mw_kernel, :llm_provider, LlmMock)
```

## Phase 3+ Integration

- [ ] Implement actual OpenAI HTTP client integration
- [ ] Implement Claude SDK integration
- [ ] Implement Llama local/API integration
- [ ] Add rate limiting and retry logic
- [ ] Add cost tracking and alerts
- [ ] Add usage metrics and logging
