A custom rule provider lets you keep a set of routing rules for AI websites separate from the rest of a Clash configuration. Instead of adding every domain to a long, subscription-managed rules list, you maintain a small YAML file at a stable URL and reference it through rule-providers. Mihomo downloads the file, loads its rules, and makes them available to the main configuration through a RULE-SET entry.
This arrangement is useful when you regularly adjust AI-related domains, want the same rule set on more than one device, or need to avoid editing a configuration that is regenerated from a subscription. It does not identify traffic by the name of an application installed on your computer. A domain rule matches a network destination, so an AI app may use several domains and a domain may also be used by other services. Start with the domains you have observed in connection logs, and check each change against the actual traffic path.
Build Custom Clash Rule Providers for AI Apps With YAML
Plan the provider and its rule scope
A rule provider has two parts. The provider definition tells Mihomo how to fetch and interpret a rule file; a rule in the main rules list tells Mihomo which policy to use when a connection matches that file. Keeping those parts separate makes updates easier to review, but it also means that a correctly downloaded provider has no effect unless the main rule list references it.
Before writing YAML, decide which traffic the provider should cover. A focused provider might contain domains used by a particular AI service’s website and API. Avoid putting broad shared infrastructure into the list without testing it. For example, a general content delivery or identity provider domain may serve many unrelated websites. Routing that entire domain through an AI-specific policy could affect sign-in, downloads, or ordinary browsing beyond the intended app.
Use domain rules when the destination is known by domain name. The example below uses suffix matching for subdomains and an exact match for a single host:
payload:
- DOMAIN-SUFFIX,ai.example
- DOMAIN,api.ai.example
- DOMAIN-SUFFIX,accounts.example
Replace the example domains with destinations confirmed from your own connection log or the service’s current network requirements. The file is YAML, and payload is the list key used by the provider format shown here. Keep each rule on its own line, use spaces rather than tabs for indentation, and do not add the outer rules: key to this provider file.
Rule types have different scopes. DOMAIN matches one exact domain, while DOMAIN-SUFFIX also matches subdomains. A suffix that is too broad can capture more traffic than intended; an exact rule that is too narrow can miss API or regional hosts. Avoid adding an IP range just because a connection log displays an IP address: IPs may change, be shared, or be reached through DNS behavior that affects when an IP-based rule can match.
Create the YAML rule file
Create a separate file, such as ai-apps.yaml, in a repository that you control. Keep it limited to routing rules and give the file a clear purpose. Do not copy your subscription URL, proxy credentials, private controller secret, or other sensitive configuration into a public repository. A rule list normally does not need any of those values.
A minimal file can look like this:
payload:
- DOMAIN-SUFFIX,ai.example
- DOMAIN,api.ai.example
- DOMAIN-SUFFIX,accounts.example
Use a consistent naming convention for the file and provider. In this example, the provider is named ai-apps, while the file is named ai-apps.yaml. Provider names are identifiers used by the main configuration; the file name can differ, but matching names make the setup easier to inspect.
After committing the file, use the direct raw-file URL as the provider URL. For a public GitHub repository, that URL generally follows this pattern:
https://raw.githubusercontent.com/OWNER/REPOSITORY/BRANCH/path/ai-apps.yaml
Replace every uppercase placeholder with the actual owner, repository, branch, and path. Open the resulting URL in a browser or retrieve it with a command-line HTTP client to confirm that it returns the YAML file itself. A repository’s normal page URL is not a raw YAML file URL; using it may return HTML that Mihomo cannot parse. A private repository may require authentication that a public raw URL does not provide automatically. Do not put access tokens into a shared configuration or a public URL.
When editing the file later, keep the format consistent. A malformed line, incorrect indentation, or an unexpected top-level key can prevent the provider from loading. Make a small change, check the committed raw file, then inspect Mihomo’s provider status before adding many more rules. This separates a formatting problem from a routing-policy problem.
Connect the provider to Mihomo
Add a provider definition and a RULE-SET rule to the main configuration. The following is a structural example: replace the URL and the proxy-group name with values that exist in your active configuration.
rule-providers:
ai-apps:
type: http
behavior: domain
format: yaml
url: https://raw.githubusercontent.com/OWNER/REPOSITORY/main/rules/ai-apps.yaml
path: ./ruleset/ai-apps.yaml
interval: 86400
rules:
- RULE-SET,ai-apps,AI-Proxy
- MATCH,PROXY
type: http tells Mihomo to retrieve the provider over HTTP or HTTPS. behavior: domain indicates that the provider contains domain rules, and format: yaml declares the file format used by the example. The local path is where the client’s core can store the downloaded file; use a path supported by the client and avoid pointing it to a directory that does not exist or is not writable. interval is the refresh interval in seconds, so 86400 requests an update every 24 hours.
The provider name in RULE-SET,ai-apps,AI-Proxy must exactly match the key under rule-providers. The final argument, AI-Proxy, must be the name of an existing proxy group or other valid policy in the active configuration. If your group is named AI Services, use that exact name instead. YAML key names, provider identifiers, and policy names should be checked for spelling and case consistency.
Place the RULE-SET entry before general rules that could already match the same connection. Mihomo processes rules in order and uses the first matching rule. If a broad DOMAIN-SUFFIX, GEOSITE, or other rule appears first and sends the connection to a different policy, a later AI provider rule will not override that decision. The final MATCH line is a fallback, not a place for the provider rule.
The provider definition and the rule list belong to the main configuration, not to the provider file itself. If a client generates its active YAML from a subscription, direct edits may be overwritten on the next update. Use that client’s supported override or merge mechanism to preserve the provider definition and its RULE-SET entry. Keep the remote rule file independently maintained, then confirm that the client actually merges your local additions into the running configuration.
| Setting | Purpose | Check before loading |
|---|---|---|
behavior |
Describes the kind of rules in the provider. | Use domain for the domain-rule example. |
format |
Declares how the provider file is structured. | Use yaml for a file with a payload list. |
url |
Identifies the remote file Mihomo fetches. | Confirm that it returns the raw YAML, not a web page. |
path |
Stores the fetched provider file locally. | Check that the client’s core can write to the chosen location. |
interval |
Sets the periodic refresh interval in seconds. | Use an integer and allow enough time between updates. |
Verify downloads and rule matches
After saving the configuration, reload it using the client’s configuration controls and inspect the Mihomo log. A successful configuration reload only proves that the main YAML parsed; it does not necessarily prove that the remote provider downloaded successfully or that a test connection matched it. Check the provider’s status in the client’s rule-provider or configuration view, if available, and look for download, parsing, or update errors in the core log.
Then test one known domain rather than opening several apps at once. Generate a new connection to a domain in the provider, find that connection in the client’s log, and check the matched rule and selected policy. A useful test record includes the requested hostname, the rule shown by Mihomo, the policy group, and the actual proxy selected by that group. If the connection uses a reused session, close and reopen the relevant app or request a host that has not already been cached.
Use this sequence to narrow down a failure:
- Check the URL: confirm that the raw URL is reachable from the device running Mihomo and returns YAML content rather than HTML or an access-denied page.
- Check the provider status: distinguish a failed download from a provider that downloaded but contains no usable rules.
- Check the local path: verify that the configured core can create or update the provider file at the selected path.
- Check the rule syntax: compare the provider’s declared behavior and format with the file’s actual structure and rule types.
- Check ordering: make sure no earlier rule matches the test connection before the
RULE-SETentry. - Check the policy name: confirm that the target proxy group exists and that it has an available selection.
If the provider status shows an old update time, first test the URL outside the client. A changed branch name, renamed file, incorrect repository path, temporary network failure, or rate limit can prevent refreshes. The core may continue using a previously downloaded copy, so old rules can appear to work even while the latest version is unavailable. Do not assume that editing the repository updates every device instantly; the configured interval and the client’s provider-update controls determine when a new copy is fetched.
If the provider downloads but a domain does not match, inspect the exact hostname in the connection log. A rule for DOMAIN,api.ai.example does not match chat.ai.example; a suffix rule for ai.example can match both subdomains and the base domain according to suffix matching. Also verify that the request actually enters the Mihomo instance being inspected. Another proxy, a browser extension, an application-specific proxy setting, or a different active client may send the traffic elsewhere.
DNS and connection metadata can affect what is visible to a rule. Domain rules need a domain name to match. If the application connects directly to an IP address, the domain provider may not match that connection as expected. DNS mode, traffic sniffing, and the way the client captures traffic can influence whether Mihomo can identify a hostname. Check the connection details before adding IP rules as a workaround; an IP address observed once is not necessarily a stable identifier for the service.
Maintain the rule set safely
Treat the provider as routing policy, not as an automatically complete catalog of every domain an AI product might use. Services can change API hosts, authentication flows, download endpoints, and regional infrastructure. When a request fails, use the connection log to identify the destination and determine whether the failure is caused by a missing rule, an unavailable node, DNS resolution, or an application error. Add a domain only after confirming that routing it through the intended policy is appropriate.
Keep changes reviewable. Prefer one rule per line, descriptive commit messages, and small updates that can be tested individually. If a newly added suffix captures unrelated traffic, remove or narrow that rule rather than compensating with a growing set of exceptions. A provider should be understandable without requiring readers to infer why a broad wildcard was added.
Do not put a provider rule after a catch-all rule, and do not use the provider’s existence as evidence that an application is routed through it. The relevant proof is a new connection whose log entry shows the expected hostname, the RULE-SET match, and the intended proxy group. If those details are absent, check the running configuration and active core instead of repeatedly changing the remote file.
Finally, retain a known-good version of the file and the matching main-configuration snippet. If a repository change breaks parsing or unexpectedly expands the match scope, reverting the last change is usually faster than debugging a large batch of edits. This gives you a compact, reusable AI routing list while keeping the main Clash configuration readable and the traffic decisions auditable.