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

# Exploring Configurable Parameters

> Explains configurable fields for runnables to override runtime parameters like LLM model per invocation, enabling cost control and flexible per-request model selection.

This lesson explains configurable fields — a mechanism for passing runtime parameters to runnables (for example, switching the LLM model used for a specific invocation). Configurable fields let you avoid hard-coding options when initializing a runnable and instead override them per invocation. This is useful for cost control (default to a cheaper model) and flexibility (upgrade to a stronger model when needed).

## Why use configurable fields?

* Avoid reinitializing runnables to change runtime behavior.
* Control costs by selecting cheaper defaults and overriding on demand.
* Compose overrides with dynamic prompts and memory for flexible workflows.

## Quick overview

1. Import required classes.
2. Register a configurable field on a runnable (e.g., `model_name`).
3. Compose the runnable with a prompt.
4. Invoke normally (uses default) or override with `with_config(configurable={...})`.

## Imports and setup

```python theme={null}
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.runnables import ConfigurableField
```

## Register a configurable field on a runnable

Initialize the ChatOpenAI runnable with a default model (for example `gpt-3.5-turbo`) and expose `model_name` as a configurable field that can be overridden at invocation time:

```python theme={null}
model = ChatOpenAI(model_name="gpt-3.5-turbo").configurable_fields(
    model_name=ConfigurableField(
        id="model_name",
        name="model name",
        description="The GPT model to use for chat",
    )
)
```

## Create a prompt template

Wrap the template text in backticks in prose to avoid MDX parsing issues:

```python theme={null}
prompt = PromptTemplate.from_template("Write a Haiku on {subject}")
```

## Compose the chain and invoke (default)

Compose the prompt and the runnable, then invoke normally. This uses the runnable's default model (`gpt-3.5-turbo`):

```python theme={null}
chain = prompt | model

# Invoke with the default (gpt-3.5-turbo)
result_default = chain.invoke({"subject": "cat"})
print(result_default)
```

Example output (using the default model):

```python theme={null}
AIMessage(content="Whiskers soft and fine\nPurring gently in the sun\nGraceful feline friend", response_metadata={'model_name': 'gpt-3.5-turbo', 'token_usage': {'completion_tokens': 19, 'prompt_tokens': 13, 'total_tokens': 32}})
```

## Override the configurable field at runtime

Use `with_config(configurable={...})` to pass overrides for registered configurable fields. Keys in the dictionary must match the configurable field ids you registered (in this example, `"model_name"`):

```python theme={null}
# Force the chain to use GPT-4 for this invocation
result_gpt4 = chain.with_config(configurable={"model_name": "gpt-4"}).invoke({"subject": "cat"})
print(result_gpt4)
```

Example output (overridden to use GPT-4):

```python theme={null}
AIMessage(content="Soft purr in the night,\nEyes gleaming in moon's soft light,\nCat in calm delight.", response_metadata={'model_name': 'gpt-4', 'token_usage': {'completion_tokens': 22, 'prompt_tokens': 13, 'total_tokens': 35}})
```

<Callout icon="lightbulb" color="#1CB2FE">
  The configurable field `id` you register must match the parameter name the runnable expects at runtime (for example, `model_name` above). Use `with_config(configurable={...})` to override values per invocation.
</Callout>

## Practical considerations

| Topic           | Guidance                                                             | Example                                                           |
| --------------- | -------------------------------------------------------------------- | ----------------------------------------------------------------- |
| Cost management | Default to a cheaper model and override only when needed             | Default: `gpt-3.5-turbo`; override to `gpt-4` for complex prompts |
| Key matching    | Ensure configurable field ids match runnable parameter names         | Use `model_name` to override the `ChatOpenAI` model parameter     |
| Composition     | Combine configurable overrides with dynamic prompt inputs and memory | Use runtime `model_name` + user-specific prompts + stored memory  |
| Debugging       | Inspect `response_metadata` to confirm which model was used          | Check `response_metadata['model_name']` in the AIMessage          |

## When to use configurable fields

* You want per-invocation control over LLM models or other runtime settings.
* Your application serves varied tasks that require different capability levels.
* You want to avoid reinitializing runnables or changing deployment configuration mid-run.

## Links and references

* OpenAI Models Overview: [https://platform.openai.com/docs/models/overview](https://platform.openai.com/docs/models/overview)
* LangChain (core concepts and runnables): [https://langchain.readthedocs.io/](https://langchain.readthedocs.io/) (or the relevant repo/docs for your distribution)

Try different OpenAI model names from the models list to see how outputs and token usage change. This pattern provides runtime flexibility without changing deployed code.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/langchain/module/abd1e527-3f6e-4e04-b421-3b1f8de5c69d/lesson/d20a74d2-983f-41da-a5bb-026e3d541199" />
</CardGroup>
