# How the Agent-Reach Configure Command Parses Twitter Cookie Input in Multiple Formats

> Learn how Agent-Reach configures Twitter cookie input. This command parses full header strings or separate tokens to extract auth_token and ct0, simplifying Twitter cookie management.

- Repository: [Pnant/Agent-Reach](https://github.com/Panniantong/Agent-Reach)
- Tags: internals
- Published: 2026-07-16

---

**TLDR:** The `agent-reach configure twitter-cookies` command delegates to the `_parse_twitter_cookie_input` helper in [`agent_reach/cli.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/cli.py), which detects whether you supplied a full cookie header string or two separate token values and extracts `auth_token` and `ct0` accordingly.

When setting up Twitter authentication for the Agent-Reach CLI, you can supply credentials using either a raw browser cookie header or two discrete tokens. The repository's parsing logic, implemented in `Panniantong/Agent-Reach`, automatically identifies which format you provided and extracts the required fields without manual conversion.

## The Two Accepted Input Formats

The `_parse_twitter_cookie_input` function recognizes inputs by scanning for specific substring patterns and delimiters.

### Full Cookie Header Format

This style accepts a string copied directly from a browser's developer tools Network tab. The parser validates this format by confirming the presence of both `auth_token=` and `ct0=` substrings anywhere in the string. It normalizes semicolons to spaces, splits on whitespace, and extracts the values following each key.

**Example input:**

```

auth_token=ABC123; ct0=DEF456; other=value

```

### Separate Token Values Format

This style accepts two whitespace-separated tokens that contain no equals signs. The parser validates this by verifying the string lacks `=` characters and splits into exactly two parts. The first token maps to `auth_token`, the second to `ct0`.

**Example input:**

```

ABC123 DEF456

```

## Implementation Details in agent_reach/cli.py

The core logic resides in the `_parse_twitter_cookie_input` function defined in [`agent_reach/cli.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/cli.py). According to the source code, the function applies a strict two-phase detection strategy:

1. **Header Detection:** If `"auth_token="` and `"ct0="` both appear in the input, the function executes `value.replace(";", " ").split()` to tokenize the string. It then iterates through tokens, using `part.split("=", 1)[1]` to extract values for keys starting with `auth_token=` and `ct0=`.

2. **Token Pair Detection:** If the header pattern is absent, the function checks `len(value.split()) == 2` and confirms `"=" not in value`. When true, it assigns `parts[0]` to `auth_token` and `parts[1]` to `ct0`.

If neither condition is satisfied, the function returns `(None, None)`, causing the CLI to print an error specifying the accepted formats. Unit tests in [`tests/test_cli.py`](https://github.com/Panniantong/Agent-Reach/blob/main/tests/test_cli.py) verify both parsing branches and edge cases.

**Source implementation:**

```python
def _parse_twitter_cookie_input(value: str):
    """Parse Twitter cookie input from either separate values or a cookie header."""
    auth_token = None
    ct0 = None

    # 1️⃣ Full header string – look for key=value pairs

    if "auth_token=" in value and "ct0=" in value:
        for part in value.replace(";", " ").split():
            if part.startswith("auth_token="):
                auth_token = part.split("=", 1)[1]
            elif part.startswith("ct0="):
                ct0 = part.split("=", 1)[1]

    # 2️⃣ Two separate values without “=”

    elif len(value.split()) == 2 and "=" not in value:
        parts = value.split()
        auth_token = parts[0]
        ct0 = parts[1]

    return auth_token, ct0

```

## Practical Usage Examples

You can invoke the configure command using either syntax:

```bash

# Format 1: Separate tokens (no equals signs)

agent-reach configure twitter-cookies ABC123 DEF456

```

```bash

# Format 2: Full cookie header (copy-paste from browser)

agent-reach configure twitter-cookies "auth_token=ABC123; ct0=DEF456; other=xyz"

```

Both commands result in `auth_token` being set to `"ABC123"` and `ct0` to `"DEF456"`.

## Configuration Persistence

Upon successful parsing, the [`agent_reach/config.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/config.py) module persists the extracted values as `twitter_auth_token` and `twitter_ct0` in your local configuration file at `~/.agent-reach/config.yaml`. Immediately after storage, the CLI executes a health check against the `twitter-cli` tool to verify the credentials are active.

## Summary

- The `_parse_twitter_cookie_input` function in [`agent_reach/cli.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/cli.py) handles all Twitter cookie parsing for the `configure` command.
- **Full cookie header** format requires both `auth_token=` and `ct0=` substrings; the parser splits on semicolons or spaces to extract values.
- **Separate values** format requires exactly two whitespace-separated tokens containing no `=` characters.
- Failed parsing returns `(None, None)` and triggers a CLI error message.
- Valid credentials are stored in `~/.agent-reach/config.yaml` and validated immediately via a health check.

## Frequently Asked Questions

### What happens if I provide only one token instead of two?

The parser checks for exactly two whitespace-separated tokens when not using the header format. Providing only one token fails the `len(value.split()) == 2` validation, causing the function to return `(None, None)` and the CLI to display an error requesting the correct format.

### Can I use spaces instead of semicolons in the cookie header?

Yes. The parser normalizes the input by executing `value.replace(";", " ")` before splitting, so both `auth_token=abc; ct0=def` and `auth_token=abc ct0=def` are valid inputs.

### Where are the parsed Twitter credentials stored?

The [`agent_reach/config.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/config.py) module writes the extracted `auth_token` and `ct0` values to `~/.agent-reach/config.yaml` under the keys `twitter_auth_token` and `twitter_ct0`.

### Does the parser validate the actual cookie values against Twitter?

No. The `_parse_twitter_cookie_input` function performs only syntactic extraction. Credential validation occurs after storage when the CLI runs a health check against the `twitter-cli` tool to confirm the tokens are active and valid.